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
Link to the challenge: