Skip to content

Commit 23c8e36

Browse files
SunJieMingSunJieMing
authored andcommitted
Merge branch 'master' of https://github.com/LambdaSchool/JavaScript-I into solution
2 parents e0bd8b5 + 05928e0 commit 23c8e36

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/arrays.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff 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;

src/callbacks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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.

0 commit comments

Comments
 (0)