-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.js
More file actions
120 lines (111 loc) · 3.86 KB
/
Copy patharrays.js
File metadata and controls
120 lines (111 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Complete the following functions.
// These functions only need to work with arrays.
// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc.
// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating
// You can use the functions that you have already written to help solve the other problems
const each = (elements, cb) => {
// Iterates over a list of elements, yielding each in turn to the `cb` function.
// This only needs to work with arrays.
// based off http://underscorejs.org/#each
for (let i = 0; i < elements.length; i++) {
cb(elements[i], i, elements); // was originally cb(elements[i]), the only reason why I refactored was because the signature for
// the cb invocation is given in underscore. No directions are given regarding following underscore, only that this exercise, or rather, just "something"
// is based off of underscorejs
}
};
const map = (elements, cb) => {
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.
const out = [];
each(elements, (el) => {
out.push(cb(el));
});
return out;
};
const reduce = (elements, cb, memo = elements.shift()) => {
// Combine all elements into a single value going from left to right.
// Elements will be passed one by one into `cb`.
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
each(elements, (el) => {
memo = cb(memo, el);
});
return memo;
};
const find = (elements, cb) => {
// Look through each value in `elements` and pass each element to `cb`.
// If `cb` returns `true` then return that element.
// Return `undefined` if no elements pass the truth test.
//
//
// REVIEW PREVIOUS COMMIT VERSION USING BUILT EACH FUNCTION AND BRING UP DURING A Q & A
//
//
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i])) {
return elements[i];
}
}
return undefined;
};
const filter = (elements, cb) => {
// Similar to `find` but you will return an array of all elements that passed the truth test
// Return an empty array if no elements pass the truth test
const out = [];
each(elements, (el) => {
if (cb(el) === true) out.push(el);
});
return out;
};
const flatten = (elements) => {
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
// we want to recursively flatten layers
/*
* establish base case
* if array has no arrays as elements return array (test for 'has or has not' involves iteration)
* create master array
* for each element in array
* combine the result of flatten array to master array
* return flatten on master array
*
* alternative
*
*
*
* the recursive call will return a number while base case returns an array
*/
// easiest case [1] => [1]
// next easiest case [1, [2,2,3]] etc. => [1,2]
// [1]
// [1,[2]]
// create empty output array // []
// if the current input with var name array is not an array
// assign output array to [input]
// return output array
// iterate over inputted array(for each) // elements = [2,2,3], curr = 2 out2 = [2,2,3]
// concatenate to output array the return value of running flatten for the current input // output = [1]
// return output array
//
// new stack
// iterate over [2,2,3]
//
// [1,[2]]
let newArr = []; // this is what the function will at the end return
if (!Array.isArray(elements)) { // elements = [1] // els2 = [2]
newArr = [elements]; //
return newArr;
}
elements.forEach((el) => { // newArr = [1], el = [2] // el = 2
newArr = newArr.concat(flatten(el));// newArr = [1].concat([2]) => [1,2]
});
return newArr;
};
/* eslint-enable no-unused-vars, max-len */
module.exports = {
each,
map,
reduce,
find,
filter,
flatten
};