Where do these values come from and how does this work?

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]
  }
  console.log(product)
}
  // Only change code above this line
  return product;
}

multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

the console displays the following values: 2,24,5040, how does this happen?

Quick response, as I am away from my keyboard.

Product is multiplied by the set of numbers each time

Product is 1

1 * [1 * 2 ] = 2

Product is 2

2 * [3 * 4] = 24

Product is 24

24 * [5 * 6 * 7] = 5040

Final Product is 5040

1 Like

no, I don’t understand this

In the function, arr is a list of array’s, each of those nested array’s contains numbers.

[
   [1, 2], 
   [3, 4], 
   [5, 6, 7]
]

The first for loop iterates over the outer array, so the variable i is an index number that references one of the inner arrays.

The second for loop then iterates over the current inner array, as referenced by arr[i]. On each iteration of the inner array, it multiplies the current value of product with the number in the inner array, and saves the result back into product.

After it is done iterating the inner loop, it logs the current value of product.

Give the variables more verbose name and add some extra variables to make it more readable, then follow the code, substituting the values as you go.

function multiplyAll(listOfLists){
    let product = 1;
    // Only change code below this line
    for (let listIndex = 0; listIndex < listOfLists.length; listIndex++){
        let listOfNumbers=listOfLists[listIndex];
        for (let numberIndex = 0; numberIndex < listOfNumbers.length; numberIndex++){
            let currentNumber=listOfNumbers[numberIndex];
            product = product * currentNumber;
        }
        console.log(product)
    }
    // Only change code above this line
    return product;
}

Following the code:

let product = 1;

Set product=1

for (let listIndex = 0; listIndex < listOfLists.length; listIndex++){

First iteration, listIndex=0

let listOfNumbers=listOfLists[listIndex];

Set listOfNumbers equal to listOfLists[0]
Now listOfNumbers=[1, 2]

for (let numberIndex = 0; numberIndex < listOfNumbers.length; numberIndex++){

First iteration of inner loop. numberIndex=0

let currentNumber=listOfNumbers[numberIndex];

Set currentNumber equal to listOfNumbers[0].
Now currentNumber=1

product = product * currentNumber;

Multiply product (1) by currentNumber (1), store the result (1) into product.
Now, product=1

for (let numberIndex = 0; numberIndex < listOfNumbers.length; numberIndex++){

Second iteration of inner loop. numberIndex++
Now numberIndex=1

let currentNumber=listOfNumbers[numberIndex];

Set currentNumber equal to listOfNumbers[1].
Now currentNumber=2

product = product * currentNumber;

Multiply product (1) by currentNumber (2), store the result (2) into product.
Now, product=2

console.log(product)

Log the value of product (2) to the console.

for (let listIndex = 0; listIndex < listOfLists.length; listIndex++){

Second iteration, listIndex++
Now listIndex=1

let listOfNumbers=listOfLists[listIndex];

Set listOfNumbers equal to listOfLists[1]
Now listOfNumbers=[3, 4]

for (let numberIndex = 0; numberIndex < listOfNumbers.length; numberIndex++){

First iteration of inner loop. numberIndex=0

let currentNumber=listOfNumbers[numberIndex];

Set currentNumber equal to listOfNumbers[0].
Now currentNumber=3

product = product * currentNumber;

Multiply product (2) by currentNumber (3), store the result (6) into product.
Now, product=6

for (let numberIndex = 0; numberIndex < listOfNumbers.length; numberIndex++){

Second iteration of inner loop. numberIndex++
Now numberIndex=1

let currentNumber=listOfNumbers[numberIndex];

Set currentNumber equal to listOfNumbers[1].
Now currentNumber=4

product = product * currentNumber;

Multiply product (6) by currentNumber (4), store the result (24) into product.
Now, product=24

console.log(product)

Log the value of product (24) to the console.

And keep going…

You need to learn to read code like this if you want to learn to program. Code reading and analysis is something that applies to all languages, not just JavaScript.

You have an array of arrays. You’ve got two loops, one through the outer list, then for each instance of that list, it loops through the array that it contains. At the end of each iteration of the outer loop, it prints a total.

So it’s
1 x 1 = 1
1 x 2 = 2
Print “2”

Then
2 x 3 = 6
6 x 4 = 24
Print “24”

Then
24 x 5 = 120
120 x 6 = 720
720 x 7 = 5040
Print “5040”

If you were expecting to see 2,6,210, then you need to clear out the product value before the inner loop, but then the name of the method wouldn’t be accurate because you wouldn’t be multiplying all those values, just each grouping of values…

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

yes, thank you, it became a little clear to me how it works, but the code turns out to be huge and I still don’t remember everything)))

I really can’t figure it out here.