Could someone please explain to me how the program knows there's 3 numbers to multiple in the 2nd element of the array?

Tell us what’s happening:

  **Your code so far**

function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i =0; i < arr.length; i++) {
  for (var j = 0; j < arr[i].length; j++) {
    product *= arr[i][j]
  }
} 
// 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/89.0.4389.114 Safari/537.36.

Challenge: Nesting For Loops

Link to the challenge:

Hi @vis.rr !

Welcome to the forum!

I have edited your post to include the spoiler tags since this is a full working solution.

I am not quite sure if I understand your question but it sounds like there is some confusion on how nested for loops work.

So it just takes times to get used to how they work. But you will have plenty of chances to work with them throughout the FCC curriculum.

I think the key thing to remember is that product doesn’t’ just stay at 1. It is constantly being updated.

When we start arr[0][0] is the first number in the first nested array which is 1.

How the math works

  • when i=0
  • product = 1 * 1(arr[0][0]), the result for product is still 1
  • product = 1 * 2(arr[0][1]), the result for product is now updated to 2
  • when i=1
  • product = 2 * 3(arr[1][0]), product is now updated to 6
  • product = 6 * 4(arr[1][1]), the result for product is now updated to 24
  • when i=2
  • product = 24 * 5(arr[2][0]), product is now updated to 120
  • product = 120 * 6(arr[2][1]), the result for product is now updated to 720
  • product = 720 * 7(arr[2][2]), the result for product is now updated to 5040

Hope that makes sense!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.