Tell us what’s happening:
So the code below did not pass the test to pass the test the condition should say if(n <= 0).
But when i paste the code below into chrome console i get 7 printing out.
Could anyone please explain why 7 prints out when the condition (n <0) is used and why 5 prints out when the condition (n <=0) is used.
Thanks.
Your code so far
function sum(arr, n) {
// Only change code below this line
if (n < 0) {
return arr[0];
}
return sum(arr, n - 1) + arr[n];
// Only change code above this line
}
sum([2, 3, 4], 1); // should return 5
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 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