Skip to content

Commit

Permalink
fix reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
Allocene committed Nov 18, 2023
1 parent a7d8b3b commit 6169371
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions homeworks/artur.kobyliatsky_Allocene/7-arrays-loops/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ console.log(onlyPositiveNumbers(arrayFive)); // [1, 3, 5];

// customReduce
function customReduce(array, reducer, initialValue) {
let accumulator = initialValue;
for (let i = 0; i < array.length; i++) {
const isInitialValue = initialValue !== undefined;
const startIndex = isInitialValue ? 0 : 1;
let accumulator = isInitialValue ? initialValue : array[0];

for (let i = startIndex; i < array.length; i++) {
accumulator = reducer(accumulator, array[i]);
}

return accumulator;
}

Expand Down

0 comments on commit 6169371

Please sign in to comment.