In the following code I cant seem to conceptualize the "multiply(arr, n-1) + arr[n - 1]… I understand the arr[n - 1] is due to the fact the arr starts at [0], and I understand you are subtracting 1 every time, but the fact that the function calls itself before doing anything else is throwing me off. I can’t seem to see where the recursive function holds a value and waits for the new value to multiply itself by. An example like this makes a lot more sense to me…
function sumRange(num){
if ( num === 1) return 1;
return num = sumRange(num - 1);
}
Your code so far
function sum(arr, n) {
// Only change code below this line
if(n <= 0) {
return 0;
} else {
return sum(arr, n - 1) + arr[n - 1];
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Replace Loops using Recursion
Link to the challenge: