|
3 | 3 |
|
4 | 4 | /* eslint-disable no-unused-vars, max-len */ |
5 | 5 |
|
| 6 | +// const each = (elements, cb) => elements.forEach((el, index) => cb(el, index)); |
6 | 7 | const each = (elements, cb) => { |
7 | 8 | // Iterates over a list of elements, yielding each in turn to the `cb` function. |
8 | 9 | // This only needs to work with arrays. |
9 | 10 | // based off http://underscorejs.org/#each |
| 11 | + for (let i = 0; i < elements.length; i++) { |
| 12 | + cb(elements[i], i); |
| 13 | + } |
10 | 14 | }; |
11 | 15 |
|
| 16 | +// const map = (elements, cb) => elements.map(el => cb(el)); |
12 | 17 | const map = (elements, cb) => { |
13 | 18 | // Produces a new array of values by mapping each value in list through a transformation function (iteratee). |
14 | 19 | // Return the new array. |
| 20 | + const arr = []; |
| 21 | + for (let i = 0; i < elements.length; i++) { |
| 22 | + arr.push(cb(elements[i], i)); |
| 23 | + } |
| 24 | + return arr; |
15 | 25 | }; |
16 | 26 |
|
17 | | -const reduce = (elements, cb, memo) => { |
| 27 | +// const reduce = (elements, cb, memo = 0) => elements.reduce((tot, el) => cb(tot, el), memo); |
| 28 | +const reduce = (elements, cb, memo = 0) => { |
18 | 29 | // Combine all elements into a single value going from left to right. |
19 | 30 | // Elements will be passed one by one into `cb`. |
20 | 31 | // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value. |
| 32 | + // const result = arrayMethods.reduce(arr, (memo, num) => (memo + num), 10); |
| 33 | + let sum = memo; |
| 34 | + |
| 35 | + for (let i = 0; i < elements.length; i++) { |
| 36 | + sum = cb(sum, elements[i]); |
| 37 | + } |
| 38 | + return sum; |
21 | 39 | }; |
22 | 40 |
|
| 41 | +// const find = (elements, cb) => elements.find(el => cb(el)); |
23 | 42 | const find = (elements, cb) => { |
24 | 43 | // Look through each value in `elements` and pass each element to `cb`. |
25 | 44 | // If `cb` returns `true` then return that element. |
26 | 45 | // Return `undefined` if no elements pass the truth test. |
| 46 | + let val; |
| 47 | + |
| 48 | + for (let i = 0; i < elements.length; i++) { |
| 49 | + if (cb(elements[i])) { |
| 50 | + val = elements[i]; |
| 51 | + break; |
| 52 | + } |
| 53 | + val = undefined; |
| 54 | + } |
| 55 | + return val; |
27 | 56 | }; |
28 | 57 |
|
| 58 | +// const filter = (elements, cb) => elements.filter(el => cb(el)); |
29 | 59 | const filter = (elements, cb) => { |
30 | 60 | // Similar to `find` but you will return an array of all elements that passed the truth test |
31 | 61 | // Return an empty array if no elements pass the truth test |
| 62 | + const passed = []; |
| 63 | + for (let i = 0; i < elements.length; i++) { |
| 64 | + if (cb(elements[i])) passed.push(elements[i]); |
| 65 | + } |
| 66 | + return passed; |
32 | 67 | }; |
33 | 68 |
|
34 | | -const flatten = (elements) => { |
| 69 | +const flatten = elements => elements.reduce( |
| 70 | + (arr, el) => arr.concat(Array.isArray(el) ? flatten(el) : el), []); |
35 | 71 | // Flattens a nested array (the nesting can be to any depth). |
36 | 72 | // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; |
37 | | -}; |
| 73 | + |
38 | 74 |
|
39 | 75 | /* eslint-enable no-unused-vars, max-len */ |
40 | 76 |
|
|
0 commit comments