@@ -14,12 +14,16 @@ const each = (elements, cb) => {
1414 // This only needs to work with arrays.
1515 // You should also pass the index into `cb` as the second argument
1616 // based off http://underscorejs.org/#each
17+ for ( let i = 0 ; i < elements . length ; i ++ ) {
18+ cb ( elements [ i ] ) ;
19+ }
1720} ;
1821
1922const map = ( elements , cb ) => {
2023 // Do NOT use .map, to complete this function.
2124 // Produces a new array of values by mapping each value in list through a transformation function (iteratee).
2225 // Return the new array.
26+
2327} ;
2428
2529const reduce = ( elements , cb , startingValue ) => {
@@ -28,6 +32,12 @@ const reduce = (elements, cb, startingValue) => {
2832 // Elements will be passed one by one into `cb` along with the `startingValue`.
2933 // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
3034 // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
35+ let memo = elements . shift ( ) ;
36+ if ( startingValue !== undefined ) memo = cb ( memo , startingValue ) ;
37+ for ( let i = 0 ; i < elements . length ; i ++ ) {
38+ memo = cb ( memo , elements [ i ] ) ;
39+ }
40+ return memo ;
3141} ;
3242
3343const find = ( elements , cb ) => {
@@ -48,6 +58,15 @@ const filter = (elements, cb) => {
4858const flatten = ( elements ) => {
4959 // Flattens a nested array (the nesting can be to any depth).
5060 // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
61+ let newArr = [ ] ;
62+ for ( let i = 0 ; i < elements . length ; i ++ ) {
63+ if ( Array . isArray ( elements [ i ] ) ) {
64+ newArr = newArr . concat ( flatten ( elements [ i ] ) ) ;
65+ } else {
66+ newArr . push ( elements [ i ] ) ;
67+ }
68+ }
69+ return newArr ;
5170} ;
5271
5372/* eslint-enable no-unused-vars, max-len */
0 commit comments