Tell us what’s happening:
I actually got the question correct, but I wanted to understand how the array is involved in the recursive function. I understand that n is subtracted by 1 through each recursion until it reaches its base case but how exactly is the function adding the number from the array if it does not have a for loop to run through it?
Your code so far
function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return arr[0];
} else {
return sum(arr, n - 1) + arr[n];
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
.
Challenge: Replace Loops using Recursion
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion