Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Max call stack is greater in while loop

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;
    }
}

enter image description here

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}`);
} 

enter image description here

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!