简体   繁体   English

为什么这段代码不起作用? (关于HTML格式)

[英]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. 我的视觉工作室代码告诉我“else if”中的“else”具有预期的声明或声明。 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 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. 你忘了关闭第一个for循环的标签。

 <!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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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