Hi people, just a clarification here, I’m ok with the exercise, it all works fine and I pass the lesson but there’s something I’m confused about.
If I set return 1 on the if, why the final result adds 1 to the else? Shouldn’t that condition be skipped as long as n is not equal or less than 0?
Sorry if the question is dumb but there’s so much stuff I’m trying to learn I might be missing something.
function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return 0; //<------ here
} else {
return sum(arr, n-1) + arr[n -1];
}
// Only change code above this line
}
sum([2, 3, 4, 5], 3)
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36
Challenge: Replace Loops using Recursion
Link to the challenge: