|
1 | | -// Complete the following functions. |
2 | | -// These functions only need to work with arrays. |
3 | | -// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc. |
4 | | -// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating |
5 | | -// You can use the functions that you have already written to help solve the other problems |
6 | | - |
7 | 1 | const each = (elements, cb) => { |
8 | | - // Iterates over a list of elements, yielding each in turn to the `cb` function. |
9 | | - // This only needs to work with arrays. |
10 | | - // You should also pass the index into `cb` as the second argument |
11 | | - // based off http://underscorejs.org/#each |
| 2 | + for (let i = 0; i < elements.length; i++) { |
| 3 | + cb(elements[i], i); |
| 4 | + } |
12 | 5 | }; |
13 | 6 |
|
14 | 7 | const map = (elements, cb) => { |
15 | | - // Produces a new array of values by mapping each value in list through a transformation function (iteratee). |
16 | | - // Return the new array. |
| 8 | + const newArray = []; |
| 9 | + for (let i = 0; i < elements.length; i++) { |
| 10 | + newArray.push(cb(elements[i])); |
| 11 | + } |
| 12 | + return newArray; |
17 | 13 | }; |
18 | 14 |
|
19 | 15 | const reduce = (elements, cb, startingValue) => { |
20 | | - // Combine all elements into a single value going from left to right. |
21 | | - // Elements will be passed one by one into `cb` along with the `startingValue`. |
22 | | - // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument. |
23 | | - // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value. |
| 16 | + const newElements = elements.slice(); |
| 17 | + if (startingValue === undefined) { |
| 18 | + startingValue = newElements.shift(); |
| 19 | + } |
| 20 | + let memo = startingValue; |
| 21 | + each(newElements, (el) => { |
| 22 | + memo = cb(memo, el); |
| 23 | + }); |
| 24 | + return memo; |
24 | 25 | }; |
25 | 26 |
|
26 | 27 | const find = (elements, cb) => { |
27 | | - // Look through each value in `elements` and pass each element to `cb`. |
28 | | - // If `cb` returns `true` then return that element. |
29 | | - // Return `undefined` if no elements pass the truth test. |
| 28 | + for (let i = 0; i < elements.length; i++) { |
| 29 | + if (cb(elements[i])) { |
| 30 | + return elements[i]; |
| 31 | + } |
| 32 | + } |
30 | 33 | }; |
31 | 34 |
|
32 | 35 | const filter = (elements, cb) => { |
33 | | - // Similar to `find` but you will return an array of all elements that passed the truth test |
34 | | - // Return an empty array if no elements pass the truth test |
| 36 | + const newArr = []; |
| 37 | + for (let i = 0; i < elements.length; i++) { |
| 38 | + if (cb(elements[i])) { |
| 39 | + newArr.push(elements[i]); |
| 40 | + } |
| 41 | + } |
| 42 | + return newArr; |
35 | 43 | }; |
36 | 44 |
|
37 | 45 | /* STRETCH PROBLEM */ |
|
0 commit comments