0

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> 
3
  • 2
    Do you see an error in the console? Commented May 15, 2017 at 20:20
  • 2
    Why dont you tell us what is happening that you believe is in error. Commented May 15, 2017 at 20:20
  • Error check the console Commented May 15, 2017 at 20:21

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.