Having problem understanding the accumalator function

Tell us what’s happening:
so my problem is with the accumulator
see: freeCodeCamp Challenge Guide: Iterate Through an Array with a For Loop
in the previous one we used the total
which basiscallly means total = total + i

total += myArr[i]

so then since in this one we needed to add them together should’t it be :

product += [i] + [x];
or 
a = a [i];
b = b[x];
product = a + b;

So why is this not working?

Your code so far


function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for(var i = 0; arr.lenght; i++){
for (var x = 0; x <arr[i].length; x++){
  product += [i] + [x];
}

}
// Only change code above this line
return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.

Challenge: Nesting For Loops

Link to the challenge:

You can’t add arrays together, you’ve written something that starts 1 += [0] + [0], which makes no sense and will error at that point.

1 Like

You have a typo in your loop declaration (lenght instead of length), but that aside - I’m not sure what this line is supposed to do:

product += [i] + [x];

  • The challenge asks you to calculate the total product, but your code tries to calculate the sum instead.

  • The product variable is of type number, so you can’t add an array to it (as stated above already). Think about what values exactly you should multiply to get the total product

Hope that gave a hint.

1 Like