Skip to content

Commit b2f6218

Browse files
committed
updated WIP
1 parent 1fead6b commit b2f6218

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎src/arrays.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const map = (elements, cb) => {
2323
// Do NOT use .map, to complete this function.
2424
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
2525
// Return the new array.
26+
const newArr = [];
27+
for (let i = 0; i < elements.length; i++) {
28+
newArr.push(cb(elements[i]));
29+
}
30+
return newArr;
2631
};
2732

2833
const reduce = (elements, cb, startingValue) => {
@@ -31,6 +36,14 @@ const reduce = (elements, cb, startingValue) => {
3136
// Elements will be passed one by one into `cb` along with the `startingValue`.
3237
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
3338
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
39+
let memo = startingValue;
40+
if (memo === undefined) {
41+
memo = elements[0];
42+
}
43+
for (let i = 1; i < elements.length; i++) {
44+
memo = cb(memo, elements[i]);
45+
}
46+
return memo;
3447
};
3548

3649
const find = (elements, cb) => {

0 commit comments

Comments
 (0)