1

I am trying to reduce an array to the sum of its even values. I have been checking on the document from MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

This says that if the initial value is provided then it won't skip the 0th index, in fact it will start from index 0. My problem is that the reduce is starting with index 1. Thus, my result is incorrect. I am sure I am reading the document incorrectly or misunderstanding it. This is the note I am referring to - "Note: If initialValue is not provided, reduce() will execute the callback function starting at index 1, skipping the first index. If initialValue is provided, it will start at index 0."

Here is my code.

var array = [1,2,3,4,6,100];
var initialValue = 0;
var value = array.reduce(function(accumulator, currentValue, currentIndex, array, initialValue) {
    //console.log(accumulator);
    if( currentValue % 2 === 0) {
      accumulator += currentValue;
      //return accumulator;
    }
    return accumulator;
});
console.log(value);

Obviously, I am seeing the result 113 and not 112. I guess, it is because accumulator already has a value 1. Thus, it is adding 1 initially.

3 Answers 3

6

If you have a look again at the document, you will notice that the callbackFn only takes at most 4 variables, and the initialValue must be on the second parameter

arr.reduce(callback( accumulator, currentValue, [, index[, array]] )[, initialValue])
                   ^                                               ^

Here is the small-fixed version which returned 112 as expected

var array = [1,2,3,4,6,100];
var initialValue = 0;
var value = array.reduce(function(accumulator, currentValue, currentIndex, array) {
    //console.log(accumulator);
    if( currentValue % 2 === 0) {
      accumulator += currentValue;
      //return accumulator;
    }
    return accumulator;
}, initialValue);
console.log(value);

Sign up to request clarification or add additional context in comments.

Comments

0

This is the syntax provided by MDN for using an initial value:

 [0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => {
    return accumulator + currentValue}, 10)

The initial value in this example is 10, it should be passed as a second argument to the reduce function.

Comments

0

You're not passing the initial value parameter to the reduce function.

Like @hgb123 explained, it is the second parameter.

Futhermore, you don't need to pass all the parameters, if you don't need them. Keep your functions as simple as possible, so they'll be easier to read:

var array = [1,2,3,4,6,100];

var value = array.reduce(function(accumulator, currentValue) {
    //console.log(accumulator);
    if( currentValue % 2 === 0) {
      accumulator += currentValue;
      //return accumulator;
    }
    return accumulator;
}, 0);

console.log(value);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.