Nesting For Loops - please explain why nest only twice instead of thrice

Tell us what’s happening:
I solved this merely by trial an error instead of using logic. I understand arrays, I understand nested loops as well but if one of the sub-arrays has 3 items then why not 3 nested loops?

EDIT: maybe I get it. if one of the conditions was multiplyAll([[1,2],[3,4],[5,6,7[5,6]]) then thrice is that right?

Your code so far

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

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0.

Link to the challenge:
https://www.freecodecamp.org/challenges/nesting-for-loops

You’re missing a closing bracket on your example, but yes.

1 Like

Thanks for replying but being the moderator maybe you can influence FCC to maybe add a small line that explains that. Like a small NOTE: There are only two for loops because there was only one level of sub-array.
It seems obvious once you know it but not if you don’t.

1 Like

FCC is open source. Anyone can submit a bug report or enhancement request by creating a GitHub Issue.

This one has really frustrated me because I eventually got it right but I still don’t fully understand why. I was the same as @avneesh, I understand arrays but this was mostly just trial and error.

Your code was the same as mine except I had an } in the wrong place, but when I pasted it over to check, suddenly it was saying ‘i’ was not defined, what???

1 Like