@@ -9,38 +9,101 @@ const each = (elements, cb) => {
99 // This only needs to work with arrays.
1010 // You should also pass the index into `cb` as the second argument
1111 // based off http://underscorejs.org/#each
12+ for ( let i = 0 ; i < elements . length ; i ++ ) {
13+ cb ( elements [ i ] , i ) ;
14+ }
1215} ;
1316
1417const map = ( elements , cb ) => {
1518 // Produces a new array of values by mapping each value in list through a transformation function (iteratee).
1619 // Return the new array.
20+ const newArr = [ ] ;
21+ each ( elements , ( item , index ) => {
22+ newArr . push ( cb ( item , index ) ) ;
23+ } ) ;
24+ return newArr ;
1725} ;
1826
1927const reduce = ( elements , cb , startingValue ) => {
28+ // arr, (memo, num) => (memo + num)
2029 // Combine all elements into a single value going from left to right.
2130 // Elements will be passed one by one into `cb` along with the `startingValue`.
2231 // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
2332 // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
33+ // let reduced;
34+ // if (startingValue === undefined) {
35+ // reduced = elements[0];
36+ // for (let i = 1; i < elements.length; i++) {
37+ // reduced = cb(reduced, elements[i]);
38+ // }
39+ // } else {
40+ // reduced = startingValue;
41+ // for (let i = 0; i < elements.length; i++) {
42+ // reduced = cb(reduced, elements[i]);
43+ // }
44+ // }
45+ // return reduced;
46+ const newElements = elements . slice ( ) ;
47+ if ( startingValue === undefined ) {
48+ startingValue = newElements . shift ( ) ;
49+ }
50+ let memo = startingValue ;
51+ each ( newElements , ( el ) => {
52+ memo = cb ( memo , el ) ;
53+ } ) ;
54+ return memo ;
2455} ;
2556
57+
2658const find = ( elements , cb ) => {
2759 // Look through each value in `elements` and pass each element to `cb`.
2860 // If `cb` returns `true` then return that element.
2961 // Return `undefined` if no elements pass the truth test.
62+ let result ;
63+ for ( let i = 0 ; i < elements . length ; i ++ ) {
64+ if ( cb ( elements [ i ] ) === true ) {
65+ result = elements [ i ] ;
66+ } else {
67+ result = undefined ;
68+ }
69+ }
70+ return result ;
3071} ;
3172
3273const filter = ( elements , cb ) => {
3374 // Similar to `find` but you will return an array of all elements that passed the truth test
3475 // Return an empty array if no elements pass the truth test
76+ const newArr = [ ] ;
77+ for ( let i = 0 ; i < elements . length ; i ++ ) {
78+ if ( cb ( elements [ i ] ) === true ) {
79+ newArr . push ( elements [ i ] ) ;
80+ }
81+ }
82+ return newArr ;
3583} ;
3684
3785/* STRETCH PROBLEM */
3886
3987const flatten = ( elements ) => {
4088 // Flattens a nested array (the nesting can be to any depth).
4189 // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
90+ let outPut = [ ] ;
91+ each ( elements , ( num ) => {
92+ if ( Array . isArray ( num ) ) {
93+ outPut = outPut . concat ( flatten ( num ) ) ;
94+ } else {
95+ outPut . push ( num ) ;
96+ }
97+ } ) ;
98+ // for (let i = 0; i < elements.length; i++) {
99+ // for (let j = 0; j < elements[i].length; i++) {
100+ // newArr.push(elements[i][j]);
101+ // }
102+ // }
103+ return outPut ;
42104} ;
43-
105+ // const myArr = [1, [2], [3, [[4]]]];
106+ // console.log(flatten(myArr));
44107/* eslint-enable no-unused-vars, max-len */
45108
46109module . exports = {
0 commit comments