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

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

#1

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


#2

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


#3

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.


#4

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


#5

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???


#6

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.