|
5 | 5 | // You can use the functions that you have already written to help solve the other problems |
6 | 6 |
|
7 | 7 | const each = (elements, cb) => { |
| 8 | + for (let i = 0; i < elements.length; i++) { |
| 9 | + cb(elements[i], i); |
| 10 | + } |
8 | 11 | // Iterates over a list of elements, yielding each in turn to the `cb` function. |
9 | 12 | // This only needs to work with arrays. |
10 | 13 | // You should also pass the index into `cb` as the second argument |
11 | 14 | // based off http://underscorejs.org/#each |
12 | 15 | }; |
13 | 16 |
|
14 | 17 | const map = (elements, cb) => { |
| 18 | + const mappedArr = []; |
| 19 | + for (let i = 0; i < elements.length; i++) { |
| 20 | + const ele = cb(elements[i], i); |
| 21 | + mappedArr.push(ele); |
| 22 | + } |
| 23 | + return mappedArr; |
15 | 24 | // Produces a new array of values by mapping each value in list through a transformation function (iteratee). |
16 | 25 | // Return the new array. |
17 | 26 | }; |
18 | 27 |
|
19 | 28 | const reduce = (elements, cb, startingValue) => { |
| 29 | + let memo = startingValue; |
| 30 | + let start = 0; |
| 31 | + if (memo === undefined) { |
| 32 | + memo = elements[0]; |
| 33 | + start = 1; |
| 34 | + } |
| 35 | + for (let i = start; i < elements.length; i++) { |
| 36 | + memo = cb(memo, elements[i]); |
| 37 | + } |
| 38 | + return memo; |
20 | 39 | // Combine all elements into a single value going from left to right. |
21 | 40 | // Elements will be passed one by one into `cb` along with the `startingValue`. |
22 | 41 | // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument. |
23 | 42 | // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value. |
24 | 43 | }; |
25 | 44 |
|
26 | 45 | const find = (elements, cb) => { |
| 46 | + for (let i = 0; i < elements.length; i++) { |
| 47 | + if (cb(elements[i])) { |
| 48 | + return elements[i]; |
| 49 | + } |
| 50 | + } |
| 51 | + return undefined; |
27 | 52 | // Look through each value in `elements` and pass each element to `cb`. |
28 | 53 | // If `cb` returns `true` then return that element. |
29 | 54 | // Return `undefined` if no elements pass the truth test. |
30 | 55 | }; |
31 | 56 |
|
32 | 57 | const filter = (elements, cb) => { |
| 58 | + const result = []; |
| 59 | + for (let i = 0; i < elements.length; i++) { |
| 60 | + const ele = elements[i]; |
| 61 | + if (cb(ele)) { |
| 62 | + result.push(ele); |
| 63 | + } |
| 64 | + } |
| 65 | + return result; |
33 | 66 | // Similar to `find` but you will return an array of all elements that passed the truth test |
34 | 67 | // Return an empty array if no elements pass the truth test |
35 | 68 | }; |
36 | 69 |
|
37 | 70 | /* STRETCH PROBLEM */ |
38 | 71 |
|
39 | 72 | const flatten = (elements) => { |
| 73 | + let flatArr = []; |
| 74 | + for (let i = 0; i < elements.length; i++) { |
| 75 | + const ele = elements[i]; |
| 76 | + if (Array.isArray(ele)) { |
| 77 | + flatArr = flatArr.concat(flatten(ele)); |
| 78 | + } else { |
| 79 | + flatArr.push(ele); |
| 80 | + } |
| 81 | + } |
| 82 | + return flatArr; |
40 | 83 | // Flattens a nested array (the nesting can be to any depth). |
41 | 84 | // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; |
42 | 85 | }; |
|
0 commit comments