Logical result of using *= operator

Tell us what’s happening:

Does this statement “product *= arr[i][j];” produce the following result?

product = [1x2]x[3x4]x[5x6x7].

I am just trying to figure out the logic here. Is this statement producing the result by taking the array and multiplying the elements as it is incremented? If the Product is = to 1 wouldn’t the x= return a value each time as 1x[i] or 1x[j] and not the product of the elements being multiplied by each other?

I know I am missing something very small here but can’t wrap my head around it. I think I just do not understand the x= operator and it’s relation to i++ and j++ for ex.

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

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/80.0.3987.163 Safari/537.36.

Challenge: Nesting For Loops

Link to the challenge:

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

  1. Evaluate product
  2. Evaluate arr[i][j]
  3. Multiply the two let result = product * arr[i][j]
  4. Save the result to product product = result

I’m not sure why you are including a bunch of brackets in there. arr[i][j] is a number.

Amazing.

So the current value of “product” is stored and multiplied by the next iteration of “arr” until the loop evaluates to false then we return the final value of “product”? I think I understand now.

Many thanks

1 Like

Yep. This loop keeps updating product until you have the final answer.