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

163
Visualizações
Total up even number from 1 to A number that user enter using JavaScript and HTML

How can I display even number from the user input? My code does not reply anything.

the question

<!DOCTYPE html>
<html>

<head>
  <script>
    var num;
    var count = 0;
    var result;

    num = Number(prompt("Enter the maksimum number: ", ""));

    document.write("Sum of all even number from 1 to " + num);

    for (int i = 0; i < num; i += 2) {
      sum += i;
    }

    document.write(sum);
  </script>
</head>

<body>
</body>

</html>

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

0

JS does not have an int type for your loop to work in, if you change that to let and change count to sum to initialise it it works:

<!DOCTYPE html>
<html>

  <head>
    <script>
      var num;
      var sum = 0;
      var result;

      num = Number(prompt("Enter the maksimum number: ", ""));

      document.write("Sum of all even number from 1 to " + num);

      for (let i = 0; i < num; i += 2) {
        sum += i;
      }

      document.write(sum);

    </script>
  </head>

  <body>
  </body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. You cannot declare a variable using int. Use let or const instead.
  2. You need to declare and initialize the variable sum.

var num;

num = Number(prompt("Enter the maksimum number: ", ""));

document.write("Sum of all even number from 1 to " + num + ": ");
let sum = 0;
for (let i = 0; i < num; i += 2) {
  sum += i;
}

document.write(sum);

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