Tell us what’s happening:
Describe your issue in detail here.
So im struggling to understand why the test case sum([2, 3, 4, 5], 3) = 9
i get that we are adding the numbers at the index which is n.
but why does the index not start at 0, how-come the 4 in the array is the at the third index?
i.e 0 + 2 + 3 + 4 =9
i’ve searched the topics on this and think i grasp the recursion however if someone could please help explain whats happening in this code
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 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0
Challenge: Basic JavaScript - Replace Loops using Recursion
Link to the challenge: