Reputation: 5
I'm trying to run this code that I made in HTML involving javascript, but it's not running. Can someone point out what's wrong?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function math() {
var a = 1;
var b = 0;
var c = 0;
var sum = 0;
for (var a = 1; a <10; a++) {
sum = a+c;
c = a+b;
a = b;
}
alert(sum);
}
</script>
</head>
<body>
<script type="text/javascript">
math();
</script>
</body>
</html>
Upvotes: 0
Views: 78
Reputation: 34
Your will get a result of an infinite loop You can't loop over a when you have set a equal to 0 inside your for loop ( a=b) and you know that variable b is equal to zero
Upvotes: 0
Reputation: 569
Your for loop is an infinite loop. You never assign b
to any value but 0. The final step in each loop, therefore, is assigning a
to 0. a
will never reach 10 and the loop will never break.
Upvotes: 1
Reputation: 5397
It's an infinite loop.
You are iterating over a
, but you are changing it to 0
in the for
loop so the script never terminates.
Upvotes: 2
Reputation: 770
Your code is running but you have created an infinite loop.
You always set a = b
inside your for loop. Since b
is equal to zero, the loop will never terminate because a
is always less than 10.
Upvotes: 3