I’m trying to figure out why the following returns 10 correctly:
function recurSum(n) {
if (n <= 1)
return n;
return n + recurSum(n - 1);
}
console.log(recurSum(4)) //output 10
but when you add “n” after the recursive call in the return, you get the wrong result. The following returns 6 incorrectly
function recurSum(n) {
if (n <= 1)
return n;
return recurSum(n - 1) + n;
}
console.log(recurSum(4)) //output 6
Why does this happen?