Tell us what’s happening:
I can’t find out where I get wrong, please help me with it please.
Your code so far
function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return arr[0];
} else {
return sum(arr, n - 1) + arr[n];
}
// Only change code above this line
}
console.log(sum([1], 0))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.
First of all, you are not suppose to return an array. You were expected to return 0.
Also,
Should be arr[n-1].
It’s confusing. It’s what I learnt today. Each function call is paused in the call stack until the last call is evaluated. And it’s the result the of the last function call that will be used as the result of the previous call, on and on. When a call is done, it bubbles off from memory…
You should maybe make use of the chrome debugger tool - set a break point in the function to get a feeling for how it works.