|
3 | 3 |
|
4 | 4 |
|
5 | 5 | const each = (elements, cb) => { |
| 6 | + // making a loop and running the function cb on all the elements |
| 7 | + for (let i = 0; i < elements.length; i++) { |
| 8 | + cb(elements[i], i); |
| 9 | + } |
6 | 10 | // Iterates over a list of elements, yielding each in turn to the `cb` function. |
7 | 11 | // This only needs to work with arrays. |
8 | 12 | // based off http://underscorejs.org/#each |
9 | 13 | }; |
10 | 14 |
|
11 | 15 | const map = (elements, cb) => { |
| 16 | + // make the new array to be mapped on |
| 17 | + const newarray = []; |
| 18 | + // putting in the values for the map |
| 19 | + each(elements, (ans) => { |
| 20 | + newarray.push(cb(ans)); |
| 21 | + }); |
| 22 | + // for (let i = 0; i < elements.length; i++) { |
| 23 | + // newarray.push(cb(elements[i])); |
| 24 | + // } |
| 25 | + return newarray; |
12 | 26 | // Produces a new array of values by mapping each value in list through a transformation function (iteratee). |
13 | 27 | // Return the new array. |
14 | 28 | }; |
15 | 29 |
|
16 | 30 | const reduce = (elements, cb, memo) => { |
| 31 | + // defining total |
| 32 | + let total = 0; |
| 33 | + // looping through the array |
| 34 | + for (let i = 0; i < elements.length; i++) { |
| 35 | + // checking for a memo |
| 36 | + if (i === 0) { |
| 37 | + if (memo) total = memo; |
| 38 | + // changing the type of total if its a string |
| 39 | + else if (typeof (elements[i]) === 'string') total = ''; |
| 40 | + } |
| 41 | + total = cb(total, elements[i]); |
| 42 | + } |
| 43 | + return total; |
| 44 | + // fix this |
17 | 45 | // Combine all elements into a single value going from left to right. |
18 | 46 | // Elements will be passed one by one into `cb`. |
19 | 47 | // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value. |
20 | 48 | }; |
21 | 49 |
|
22 | 50 | const find = (elements, cb) => { |
| 51 | + let counter = 0; |
| 52 | + for (let i = 0; i < elements.length; i++) { |
| 53 | + if (cb(elements[i]) === true) { |
| 54 | + counter += 1; |
| 55 | + return elements[i]; |
| 56 | + } |
| 57 | + } |
| 58 | + if (counter === 0) return 'undefined'; |
23 | 59 | // Look through each value in `elements` and pass each element to `cb`. |
24 | 60 | // If `cb` returns `true` then return that element. |
25 | 61 | // Return `undefined` if no elements pass the truth test. |
26 | 62 | }; |
27 | 63 |
|
28 | 64 | const filter = (elements, cb) => { |
| 65 | + const arr = []; |
| 66 | + let counter = 0; |
| 67 | + for (let i = 0; i < elements.length; i++) { |
| 68 | + if (cb(elements[i]) === true) { |
| 69 | + arr[counter] = elements[i]; |
| 70 | + counter += 1; |
| 71 | + } |
| 72 | + } |
| 73 | + return arr; |
29 | 74 | // Similar to `find` but you will return an array of all elements that passed the truth test |
30 | 75 | // Return an empty array if no elements pass the truth test |
31 | 76 | }; |
32 | 77 |
|
33 | 78 | const flatten = (elements) => { |
| 79 | + let flatarr = []; |
| 80 | + each(elements, (elem) => { |
| 81 | + if (Array.isArray(elem)) { |
| 82 | + const nestedarray = flatten(elem); |
| 83 | + flatarr = flatarr.concat(nestedarray); |
| 84 | + } else { |
| 85 | + flatarr.push(elem); |
| 86 | + } |
| 87 | + }); |
| 88 | + return flatarr; |
34 | 89 | // Flattens a nested array (the nesting can be to any depth). |
35 | 90 | // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; |
36 | 91 | }; |
|
0 commit comments