The question is to calculate the sum of ten natural numbers but the code isn’t working.
<!DOCTYPE html>
<html>
<head>
<title>Sum of first 10 Natural numbers</title>
</head>
<body>
<script>
var sum = 0, n;
parseInt (prompt("Enter the number: "));
for (let i = 0; i <= n; i++) {
sum += i;
}
alert ("Sum of Numbers: " + sum);
</script>
</body>
</html>
>Solution :
The reason your code was not working is because you were not saving the value from prompt in a variable.
var sum = 0, n;
n = parseInt (prompt("Enter the number: "));
for (let i = 0; i <= n; i++) {
sum += i;
}
alert ("Sum of Numbers: " + sum);