Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!