Skip to content

Commit

Permalink
added underfined check to customReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiMorozov88 committed Nov 20, 2023
1 parent 347c4e6 commit 67295f9
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ console.log('arrayThree positive numbers:', onlyPositiveNumbers(arrayThree));
console.log('arrayFour positive numbers:', onlyPositiveNumbers(arrayFour));
console.log('arrayFive positive numbers:', onlyPositiveNumbers(arrayFive));

const testArray = [5, -6, 7, 10, 15, -7];
const testArray = [3, -6, undefined, 10, 15, -7];
function customReduce(array, reducer, initialValue) {
if (array[0] === undefined) return 'error, underfined in array';
let reducedArray = initialValue || array[0];
const startCount = initialValue ? 0 : 1;
for (let count = startCount; count < array.length; count++) {
if (array[count] === undefined) return 'error, underfined in array';
reducedArray = reducer(array[count], reducedArray);
}
return reducedArray;
Expand Down

0 comments on commit 67295f9

Please sign in to comment.