Basic JavaScript - Nesting For Loops

Hi, thank you for helping in this issue in advance.

I don’t understand why my code can’t pass. I even check the answer and it is as same as mine. Can someone help what’s wrong with my code?

Thank you so much.

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++) {
      return product = product * arr[i][j];
    }
  };
  // Only change code above this line
  return product
}
console.log(product);
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0

Challenge: Basic JavaScript - Nesting For Loops

Link to the challenge:

return product = product * arr[i][j];

this line.

Return immediately STOPS your loop, and end your function call.

2 Likes

Also, be careful that ‘product’ is a local variable of the ‘multiplyAll’ function. So, either declare it as a global variable, or since ‘multiplyAll’ returns a value, you can print the return value using.

console.log(multiplyAll([[1, 2], [3, 4], [5, 6, 7]]));
1 Like

I never thought this bug.
Thank you so much! I now learn a good lesson for using return.

Thank you for your remind, but I am not totally understand.

Do you means in the line “console.log(product);” I can code like:
“print product”? Nevertheless, it is still outside the function.

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