Replace Loops using Recursion help me

Tell us what’s happening:
Describe your issue in detail here.
I can not understand this
product *= arr[i]
, how does this code work, can you explain that to me

  **Your code so far**
function sum(arr, n) {
// Only change code below this line

// Only change code above this line
}


function multiply(arr, n) {
  let product = 1;
  for (let i = 0; i < n; i++) {
    product *= arr[i];
  }
  return product;
}

console.log(multiply([1,2,3], 3))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Replace Loops using Recursion

Link to the challenge:

The operator *= is similar to += or -=. Its a shortcut to taking a value and multiplying itself to something else. Basically, x *= 3 would be equivilent to x = x * 3.

For example.

let x = 5;
x *= 3;               // this will take x * 3, and save it in x, so x = 3 * 5
console.log(x);   //This will print 15

Now your multiply function is supposed to multiply the first n values in arr together using a loop, so for every value it uses product *= arr[i] to multiply that value in the array to product, therefore after the loop finishes, product will be all those values multiplies together. Hope that makes sense.

1 Like

So I guess when I code: console.log(multiply([1,2,3], 2)), it will work like this: product *= arr[i] means 1 * 1 *2 right

another question my friend, is that how does multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1], thank you

3 * 2 * 1 = 3 * (2 * 1)