Condense arrays with reduce 2

Tell us what’s happening:

Your code so far

var array = [4,5,6,7,8];
var singleVal = 0;
// Only change code below this line.
var singleVal = array.reduce(function(previousVal, currentVal){

  return previousVal + currentVal;

}, 0);

singleVal = array;

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36.

Link to the challenge:

How reduce works:

You do not need to declare the var twice “var singleVal”, when it’s already declared you just reference it with “singleVal”, so “singleVal = array.reduce(…”. You left a line of code in that you need to remove “singleVal = array;” which modifies “singleVal” after you’ve done the mostly correct work you need to do for the exercise.

Yeah, the last line sets single_val back to the array it was originally, so you’re undoing the work you just did.

thank you very much JohnnyBizzel, I found the link you sent me quite useful and I was able to solve the task with ease

1 Like