Condense arrays with reduce :-)

Tell us what’s happening:

Hi Guys,

I’m not really stuck, just wondering. In the example it says:

var singleVal = array.reduce(function(previousVal, currentVal) {
return previousVal - currentVal;
}, 0);

I choose not to use the ,0 part and I passed the exercise immediately. Could someone please explain to me what the reason is why the example added ,0 ?

Thanks guys.

Your code so far

var array = [4,5,6,7,8];
var singleVal = 0;

// Only change code below this line.

singleVal = array.reduce(function(previousVal, currentVal) {
  return previousVal + currentVal;
});

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/condense-arrays-with-reduce

Hello,

This comes from the definition of the Reduce fonction
Array.prototype.reduce(callback[, initialValue])

As you can see, the 0 parameter was the initialValue which is optional with 2 cases:

  1. No value passed
    The first reduce() call will have previousVal set with the first element , and currentVal set to the second element.
    In short, previousVal = arr[0] and currentVal = arr[1]

  2. Value passed
    The first reduce() call will have previousVal set with initialValue and currentVal to the first element.
    In short, previousVal = initialValue and currentVal = arr[0]

2 Likes

The second argument is for intial value of the reduce function :

reduce((accumulator, currentValue, currentIndex, array) => {
    return accumulator + currentValue;
}, 0);

in this the callback functions have optional arg. of (currentIndex and array)
the reduce function has second argument ( first one being the callback function) as the InitialValue
this means that the initial value will start from 0

other case:

reduce((accumulator, currentValue, currentIndex, array) => {
    return accumulator + currentValue;
}, 5);

The initial return value to the accumulator would be ( currentValue+5) for the first time
and then keeps on going as normal
Edit: If you still don’t understand you can checkout this site:


Hope this helps!
Cheers!
1 Like

This is technically wrong and stated in the doc you linked:

Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.

Edit: Unless the ‘start from 0’ was about the array idx :wink:

Took me a while to read it over and over what you said and was written, but I understand what you say. I wonder now, as the initial value is passed as 0, which is the first index in the array, it will do exactly the same as when you pass no initial value right? Passing an initial value would only be necessary if the index you want to start with is > 0. Do I get it right like this?

Hum well, not exactly.
Let’s get your example and see the with and without initialValue

var myArr = [4,5,6,7,8];
  1. Without initialValue
    When entering the reduce function, you’ll have
previousVal = myArr[0];
currentVal = myArr[1];
return previousVal + currentVal;

The output would be 4 + 5 => 9

  1. With initialValue = 0
    When entering the reduce function, you’ll have
previousVal = initialValue;
currentVal = myArr[0];
return previousVal + currentVal;

The output would be 0 + 4 => 4

Remember this is the first iteration of the reduce function only ! After that, it takes the array indexes and continue

2 Likes

I actually meant the initial value to be 0 ,
it was in regards of what @MarcusQ asked .

So indeed, this isn’t the truth (but it produces the same I agree)
If not set, the initial value isn’t 0, it’s the array first indice value :slight_smile:

That makes sense, thanks a lot. So in that case the 0 is not referring to an index, but a value.

I have dozen of other questions related to this, but I will figure that out myself by doing stuff and see what the result is.

Thanks again!

1 Like

Yep ! If you want to start from another index than 0, you could either use the currentIndex as in @Dsxv 's answer to return the sum

Ex: only sum for index above 3

reduce((accumulator, currentValue, currentIndex, array) => {
    if (currentIndex < 3) 
        return accumulator;
    return accumulator + currentValue;
});

You could also provide a subset of the array to the reduce function with slice for exemple

var arr = [1,2,3,4];
arr.slice(2).reduce( ... ); 

I totally didn’t see the var in the beginning… I just realized that you don’t need to create a var twice so deleting var and the 0 is the only thing you need to do and change the - into a +. However, the logic behind the method is still a little confusing.

Thanks for your help.