Tell us what’s happening:
I couldn’t get why this condition is true in the challenge:
return sum(arr,n-1) + arr[n-1];
I thought that it can be written as:
return sum(arr,n-1) + arr[n];
or
return sum(arr,n) + arr[n-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
}
var a = [1,23,3,4,5];
console.log(sum(a,5))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) 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