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

272
Visualizações
what is the value of results for the makeCounter()

What is the value of const result? my guess was (3, 1). This is confusing to me so any help would be appreciated.

    function makeCounter() {
      let count = 0;
      return function() {
        count += 1;
        return count;
      }
    }
    const counter1 = makeCounter();
    const counter2 = makeCounter();
    counter1();
    counter1();
    
    const c1 = counter1();
    const c2 = counter2();
    const result = [c1,c2];
    console.log(result)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, [3, 1] is correct. (You can select 'run code snippit' to see it in action).

The reason here is that calling makeCounter() returns a function, with a local count variable initialized as zero.

Each time you call the the function returned by makeCounter() (i.e. counter1, or counter2), that counter's count is incremented.

counter1() is called 3 times, and counter2() is called once. And they are put together in an array at the end, thus [3, 1].

about 4 years ago · Juan Pablo Isaza Relatório
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