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

224
Visualizações
I have this logic to loop over my value passed as argument and print them one letter per second
let sequenceI = 0;

function sequence(arr){
  document.getElementsByTagName('P')[0].innerHTML += arr[sequenceI];
  ++sequenceI;  
  setTimeout(() => sequence(arr), 150);
  if (sequenceI > arr.length) {
    document.getElementsByTagName('P')[0].innerHTML ="";
    sequenceI = 0;
  } 
}

sequence('Software Developer');

I have this logic to loop over my value passed as argument and print them one letter per second. Why does this not return undefined when the sequenceI becomes 18? But it rather starts the loop again

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

0

Why does this not return undefined when the sequenceI becomes 18? But it rather starts the loop again

The statement arr[sequenceI] (when sequenceI is 18) does return undefined. It just does not manage to get displayed as text on screen because you reset the text to an empty string right away.

Here is what happens when sequenceI is 18

  // 'undefined' is added to the text on screen
  document.getElementsByTagName('P')[0].innerHTML += arr[sequenceI];
  // sequenceI goes from 18 to 19
  ++sequenceI;
  // a callback is scheduled 150ms from now
  setTimeout(() => sequence(arr), 150);
  // the check is made and because 19 > 18 it resolves to true
  if (sequenceI > arr.length) {
    // the text is reset to an empty string
    document.getElementsByTagName('P')[0].innerHTML ="";
    // sequenceI is set to 0, and since there is a call scheduled 150ms from now,
    // it will just restart the whole thing from the beginning (in 150ms) 
    sequenceI = 0;
  }
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