Basic JavaScript - Nesting For Loops

Tell us what’s happening:
Describe your issue in detail here.
why am i getting a prompt that j is not defined?
Your code so far

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

Challenge: Basic JavaScript - Nesting For Loops

Link to the challenge:

Hi there and welcome to our community!

You have a syntax error in this line. A single stray punctuation mark can break your code.

1 Like

It is unfortunate that it isn’t just a syntax error and the error message is a bit confusing. But at least you get a reference error when using let, if you had been using var it wouldn’t have complained at all and the function would just return NaN.

I’m not entirely sure what the scope looks like with that stray semicolon but it must be causing the variable usage to be outside the declaration scope. This feels a bit surprising considering where the code blocks are.

I guess this is the scope of the variable

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++);

and this is outside of that scope

{ 
  product = product * arr[i][j];
}