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

216
Visualizações
how to count back with a javascript code?

This is an example exercise to count backwards from 10 to 1. Print the numbers on the screen.

and this is my code:

<script type="text/javascript">
i=1
while( 11 > i ) {
document.writeln("<p>" + i + "</p>");
i--;
}
</script>

I don't know what mistake is in the code but the numbers didn't print on the screen. Also, the browser took awhile to load and I had to stop it. Could someone give an explanation so I can understand how to write this the correct way? Thank you.

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

0

So, as I understood you are trying to add the "p" tag with iteration number to the your document. There are several mistakes in your code:

  1. You did not specify type of the variable "i" In our example, "let" will be best choise.
  2. Your while loop is endless because you are not reassigning "i" variable Your "i--" operation just took the number from variable, then subtracts 1 and does not use the result anywhere. You need to assign the result to another variable or, in this case, overwrite an existing variable
  3. Also, the loop condition is not built correctly. The final result should look like:

let i = 10;
while (i !== 0) {
  document.writeln("<p>" + i + "</p>");
  i = i - 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