 # Recursion Multiply Statement Confusion

I’m currently on the Code Camp lesson for recursion, which teaches how to multiply elements within an array using recursion.

I don’t understand the logic of this statement:

`multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]`

I get the logic of how the “for” function works doing this:

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

Is the “n-1” introduced because the array uses zero based indexing? So you wouldn’t multiply 0?

I’m just confused on how we came to the conclusion that

`multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]`

Thanks

``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36`.

Challenge: Replace Loops using Recursion

1 Like

`multiply(arr, n)` is a function that multiplies the first `n` elements of the array `arr` together.

This is the same as taking the product of the first `n-1` elements and multiplying that by the last element.

In code, we write that sentence as

``````//                  ↓ product of first n - 1 elements
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
//                                         ↑ last element in the arr
``````
2 Likes

ohhh ok. making sense now, had to write out an array to visualize it and get it in my brain but yea that makes sense now.

I wasn’t understanding that `arr[n-1]` is the last element in the array.

Super helpful, thank you so much!

2 Likes

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