@@ -9,29 +9,58 @@ 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+ for ( let i = 0 ; i < elements . length ; i ++ ) {
22+ newArr . push ( cb ( elements [ i ] ) ) ;
23+ } return newArr ;
1724} ;
1825
1926const reduce = ( elements , cb , startingValue ) => {
2027 // Combine all elements into a single value going from left to right.
2128 // Elements will be passed one by one into `cb` along with the `startingValue`.
2229 // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
2330 // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
31+ const comboV = elements . slice ( ) ;
32+ let memo ;
33+ if ( startingValue === undefined ) {
34+ memo = comboV . shift ( ) ;
35+ } else {
36+ memo = startingValue ;
37+ }
38+ each ( comboV , ( item ) => {
39+ memo = cb ( memo , item ) ;
40+ } ) ;
41+ return memo ;
2442} ;
2543
2644const find = ( elements , cb ) => {
2745 // Look through each value in `elements` and pass each element to `cb`.
2846 // If `cb` returns `true` then return that element.
2947 // Return `undefined` if no elements pass the truth test.
48+ for ( let i = 0 ; i < elements . length ; i ++ ) {
49+ if ( cb ( elements [ i ] ) === true ) {
50+ return elements [ i ] ;
51+ }
52+ }
3053} ;
3154
3255const filter = ( elements , cb ) => {
3356 // Similar to `find` but you will return an array of all elements that passed the truth test
3457 // Return an empty array if no elements pass the truth test
58+ const newArr = [ ] ;
59+ for ( let i = 0 ; i <= elements . length ; i ++ ) {
60+ if ( cb ( elements [ i ] ) === true ) {
61+ newArr . push ( elements [ i ] ) ;
62+ }
63+ } return newArr ;
3564} ;
3665
3766/* STRETCH PROBLEM */
0 commit comments