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

154
Visualizações
How to spam numbers in javascript

I want from script to do 1 2 3 4 5... But it give me 499. What I need to add to the script is not to add numbers but just to print them

function numbers() {
  for (let i = 0; i < 500; i++) {
    var spam = i + "<br>";
  }
  document.getElementById('demo20').innerHTML = spam;
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

function numbers() {
  const demo20 =  document.getElementById('demo20');
  for (let i = 0; i < 500; i++) {
   demo20.insertAdjacentHTML("beforeend", i + "<br>");
  }
}

numbers();
<div id="demo20"></div>

If you want a small delay in printing:

async function numbers() {
  const demo20 = getElementById('demo20');
  for (let i = 0; i < 500; i++) {
    demo20.getElementById('demo20').insertAdjacentHTML("beforeend", i + "<br>");
    await new Promise(res => setTimeout(res, 100));
  }
}

numbers();
<div id="demo20"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Your spam variable is constantly overwritten and at the end of the loop it has the value 499. You need to concatenate the variable with itself.

  var spam = "";
  for (let i = 0; i < 500; i++) {
    var spam += i + "<br>";
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

function numbers() {
  var spam = ''
  for (let i = 0; i < 500; i++) {
     spam += i + "<br>";
  }
  document.getElementById('demo20').innerHTML = spam;
}
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