I dont understand the answer at all

I truly don’t understand this answer. Can someone help explain this to me?

How long does it take a beginner to usually finish the first 100 tasks of java?

  **Your code so far**

function multiplyAll(arr) {
let 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 = 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/99.0.4844.74 Safari/537.36

Challenge: Nesting For Loops

Link to the challenge:

The key here since it needs to multiply everything is the accumulator and in this case the product variable that starts with a value of 1. Since, any number multiplied by 1 would have the same value.

Now, on the loops there is the inner loop (with variable of j) and the outer loop (with a variable of i).

The inner loop (with the variable j) would multiply the inner arrays one value at a time.
[1,2], [3,4], [5,6,7]

And the outer loop (with the variable i) would move to the next array to be used by the inner loop above. For example on the first loop for the outer loop
it will use [1,2] which then goes to the inner loop
the next iteration would be [3,4]
and the last iteration would be [5,6,7]

The value of the arrays goes like this:

arr[0][0] = 1
arr[0][1] = 2
arr[1][0] = 3
arr[1][1] = 4
arr[2][0] = 5
arr[2][1] = 6
arr[2][2] = 7

The first loop will process this one:
arr[0][0] = 1
arr[0][1] = 2

1 * 1 * 2 = 2

[Note: Remember the product starts with the value of 1 and the array starts also with the value of 1 thus the two 1s first]

The second loop will do this one:
arr[1][0] = 3
arr[1][1] = 4

2 * 3 * 4 = 24

[Note: The 2 is the last value of product in the last iteration]

And finally the third loop will do this one:
arr[2][0] = 5
arr[2][1] = 6
arr[2][2] = 7

24 * 5 * 6 * 7 = 5040

[Note: The 24 is the last value of product in the last iteration]

When in doubt, log it out I say.

function multiplyAll(arr) {
  let product = 1;
  for (var i = 0; i < arr.length; i++) {
    console.log('* loop i =', i)
    for (var j = 0; j < arr[i].length; j++) {
      console.log('*** loop i =', i, 'and j =', j)
      console.log('*** before product =', product, 'and arr[' + i + '][' + j + '] =', arr[i][j])
      console.log('*** multiplying', product, 'and', arr[i][j])
      product = product * arr[i][j];
      console.log('*** product after', product)
    }
  }
  console.log('returning', product)
  return product;
}

Trace through the code and understand what is happening.

How long does it take a beginner to usually finish the first 100 tasks of java?

Just to be clear, this is JavaScript, not Java - different things.

But don’t think this way. It takes what it takes. Some people struggle with one thing but ace other things. Just keep learning and getting better.

I’ll add [spoiler][/spoiler] tags to your post since it is a working answer.

Thank you, you’re totally right. Yes, this is Javascript and I will just take it one task at a time.

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