I don’t understand why this loop will ever return anything other than ‘1’. At some point while the code runs n will equal 0, in which case the program stops and returns ‘1’.
Your code so far
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36
.
Challenge: Replace Loops using Recursion
Link to the challenge: