Looking into the SICP book and JS functional programming I created two recursive functions. My expectation was that both of them raised a stack overflow error. But it is only the sumAll() function that raised the error. See below the code for both functions sumAll() and factorial():
As expected the sumAll() function did raise a stack overflow error
function sumAll(n, i = 0, result = 0) {
return (i > n)
? result
: sumAll(n, i + 1, i + result);
}
console.log(sumAll(10000));
The factorial() function below did not raise a stack overflow error:
function factorial(n){
return (n == 1)
? 1
: n* factorial((n-1))
}
console.log(factorial(10000))
My question is why the factorial() function does not raise a stack overflow and works perfectly in nodeJS meanwhile the sumAll() did raise it also in nodeJS