all.
Would be grateful if someone could clarify the following.
We have a basic factorial function and a for loop with a try catch block which runs the function until error
“Maximum call stack size is exceeded” is thrown.
When run in while loop max N is consistently 15699.
However when run without for loop, max call stack size is 11416.
Everything greater then 11416 number throws the “Maximum call stack size exceeded” error.
I would assume that the factorial function will be able to run with anything lesser than 15699, but it’s not the case.
Please see the code and screenshots of results below.
Code executed in in IDEA terminal, node -v – 14.16.1.
Would be very grateful if someone could clarify that to me.
Thank you very much in advance,
Katya
Max N of below is 15699
const nFactorial = (n) => {
if (n === 1) {
return 1;
}
return n * nFactorial (n - 1);
};
let n = 1;
while (n <= 100000) {
try {
nFactorial (n);
n++;
} catch (e) {
console.log(`${e.message} with N = ${n}`);
break;
}
}
Below code only runs with N = 11416 and throws with anything greater than that number.
const nFactorial = (n) => {
if (n === 1) {
return 1;
}
return n * nFactorial (n - 1);
};
const n = 11416;
try {
nFactorial (n);
} catch (e) {
console.log(`${e.message} with N = ${n}`);
}