Explain the parameters in a condense array

I finished the problem but it doesn’t explain much about the previousVal and currentVal. I thought the previousVal was the sum of the array so i figured it didn’t need a currentVal of 0 (zero). But i get an error when I remove the currentVal. I am curious as to why there are both the currentVal and previousVal as a parameter. Where to they come from specifically. Is the 0 in the argument the currentVal?


Your code so far

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

// Only change code below this line.

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38.

Link to the challenge:

0 is the start
previousval is the sum so far
currentval is the one to be added in this cycle


1 Like

Ahh i see, so currentVal will be 9 when the previousVal is 4. And it continues until all the values in the array has been added together.

Thanks :raised_hands: