简体   繁体   中英

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.stack.imgur.com/Gwc1c.png

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM