I don’t understand why at some point there is [ n - 1] in the example for the loop
I coppie the example here and can’t seem to find or understand at what point I would know that minus 1 comes in. So I don’t understand how I would know to put minus 1 when doing a recursive problem like this. Can someone please explain where the minus 1 comes from ?
I hope I explained my question well
function multiply(arr, n) {
let product = 1;
for (let i = 0; i < n; i++) {
product *= arr[I];
}
return product;
}
Then this is what the recursive would be
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
function sum(arr, n) {
// Only change code below this line
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15
Challenge: Basic JavaScript - Replace Loops using Recursion
Link to the challenge: