Ardent
Ardent

Reputation: 49

Why doesn't this code work? (on HTML format)

Why doesn't this code work? My visual studio code is telling me that the "else" in "else if" has declaration or statement expected. my script:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var a = 1+1;
var y = 1.5*2
if (a = 2){
for (var i = 0; i<5; i = i + 2){
    document.write("Hello "+ i +" Everyone.</br>")
} else if (y=3){
for (var j = 2; j < 10; j = j+3){
    document.write("Hey there.")
}
}
}
</script>
</body>
</html>

https://i.sstatic.net/Gwc1c.png

Upvotes: 1

Views: 57

Answers (1)

Stephen
Stephen

Reputation: 1099

Your code didn't work for a couple of reasons:

  • When comparing a variable to a number you have to use either == or === (compare value and compare value / type).
  • You forgot to close off the tags for your first for loop.

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var a = 1+1;
var y = 1.5*2

if (a === 2) {
  for (var i = 0; i<5; i = i + 2) {
    document.write("Hello "+ i +" Everyone.</br>")
   }
} else if (y === 3) {
  for (var j = 2; j < 10; j = j+3){
    document.write("Hey there.")
  }
}
</script>
</body>
</html>

Upvotes: 7

Related Questions