How can I display even number from the user input? My code does not reply anything.
<!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>
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>
int. Use let or const instead.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);