File tree Expand file tree Collapse file tree 2 files changed +8
-6
lines changed
Expand file tree Collapse file tree 2 files changed +8
-6
lines changed Original file line number Diff line number Diff line change @@ -22,12 +22,14 @@ const map = (elements, cb) => {
2222 return mappedArr ;
2323} ;
2424
25- const reduce = ( elements , cb , startingValue = elements . shift ( ) ) => {
25+ const reduce = ( elements , cb , startingValue ) => {
2626 // Combine all elements into a single value going from left to right.
27- // Elements will be passed one by one into `cb`.
28- // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
29- let memo = startingValue ;
30- each ( elements , ( item ) => {
27+ // Elements will be passed one by one into `cb` along with the `startingValue`.
28+ // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
29+ // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
30+ const elementsCopy = elements . slice ( ) ;
31+ let memo = startingValue || elementsCopy . shift ( ) ;
32+ each ( elementsCopy , ( item ) => {
3133 memo = cb ( memo , item ) ;
3234 } ) ;
3335 return memo ;
Original file line number Diff line number Diff line change 11/* eslint-disable */
22
3- /* For portion of the assignment your job is to write functions
3+ /* For a portion of the assignment your job is to write functions
44 * so that each function invocation below works. You're working backwards.
55 *
66 * There are no tests for this file.
You can’t perform that action at this time.
0 commit comments