Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda