I want to use recursion to calculate the sum of from 1 to 100000,but when I run the followings code,the browser throw a exception(Uncaught RangeError: Maximum call stack size exceeded).
code:
function sum(num){
if(num > 0){
num +=arguments.callee(num-1);
}
return num;
};
sum(100000);
function computeMaxCallStackSize() {
try {
return 1 + computeMaxCallStackSize();
} catch (e) {
// Call stack overflow
return 1;
}
};computeMaxCallStackSize();//output:15624
sum = n => (n * n + n) / 2;seems like it'd be more efficient... - zzzzBov