Nesting For Loops wondering about the multiplication part

**Tell us what’s happening:**I just have one question if someone could explain why a simple multiplication won`t work? like: product == arr[i] * arr[j];

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;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops

Notice you are using double equals here. So you aren’t storing the multiplied value into product variable. You are just comparing them.

3 Likes

In the single-line thing, you’re absolutely right, the OP compares product to arr[i]*arr[j] rather than storing it. However, the included sample function runs fine exactly as is. If you question that, I’d suggest doing a debug within your for loop:

console.log(product, "i="+i+ " j="+j+"arr[i][j]="+arr[i][j]);
1 Like

Would it work with triple equals?

when you say “simple multiplication won’t work”, what exactly do you mean? It works fine. Perhaps what you’re asking the js engine to do isn’t what you intend. If you want to see it working as written, take a look at this fiddle. Make sure you open the console.

Thanks for the help everyone!
I think I just realized that we are working with multi-dimensional array
and that is why we have to use arr[i][j] and not arr[i] * arr[j]
and by "simple multiplication " I meant something like this var total = var i * var j
but this task requires more than this simple multiplication :smiley: