Tell us what’s happening:
Hello!
I completed the challenge but I did not understand how the value of “n” behaves.
So I asked to the console and this is what I got (below)
I am sending the “n-1” value to “n” so how is this changing from 0 to 3 ?
Thank you, Davit.
// running tests
// tests completed
// console output
0
1
0
3
2
1
0
Your code so far
function sum(arr, n) {
// Only change code below this line
console.log(n);
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
.
Challenge: Replace Loops using Recursion
Link to the challenge: