JavaScript:未捕获的语法错误:else if 行上出现意外标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18967582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: Uncaught SyntaxError: Unexpected token on an else if line
提问by Gihi
this is my pretty much my first JavaScript program. I don't get why it won't work, I don't know how to debug properly, I used F12 on Google chrome to go in developer mode. If I load my html page, nothing happens and the consol says: Uncaught SyntaxError: Unexpected token else and that the error comes from line 18.
这几乎是我的第一个 JavaScript 程序。我不明白为什么它不起作用,我不知道如何正确调试,我在 Google chrome 上使用 F12 进入开发人员模式。如果我加载我的 html 页面,没有任何反应,控制台会说:Uncaught SyntaxError: Unexpected token else 并且错误来自第 18 行。
This is my whole code, seeing as the problem might not lie on line 18 alone:
这是我的全部代码,因为问题可能不仅仅在于第 18 行:
<!DOCTYPE html>
<html>
<head>
<title>
BMI calculator
</title>
</head>
<body>
<script language="JavaScript">
var leeftijd= prompt("Ben je 18 of ouder? (Ja=1)");
if (leeftijd == 1){
var gewicht= prompt("Geef je gewicht in in kilo's");
var lengte= prompt("Geef je lengte in in centimeters");
while (gewicht > 500 || gewicht < 0 || lengte > 300 || lengte < 0.4){
if (lengte > 300 || lengte < 0.4){
lengte = prompt("Geef je lengte in in kilo's");
else if (gewicht > 500 || gewicht < 0){
gewicht = prompt("Geef je gewicht in in kilo's");
}
}
}
var bmi = Math.round((gewicht / 100) / (lengte * lengte));
if (bmi >40) {
confirm("Uw bmi is" + bmi + ". U lijdt aan extreme obesitas.");
else if (bmi > 30 && bmi <=40)
confirm("Uw bmi is" + bmi + ". U lijdt aan obesitas.");
else if (bmi > 25 && bmi <=30)
confirm("Uw bmi is" + bmi + ". U lijdt aan overgewicht.");
else if (bmi > 18 && bmi <=25)
confirm("Uw bmi is" + bmi + ". U heeft een normale BMI.");
else if (bmi < 18)
confirm("Uw bmi is" + bmi + ". U lijdt aan ondergewicht.");
}
}
else {
confirm("Je moet 18 of ouder zijn om je BMI te kunnen berekenen.")
}
</script>
</body>
</html>
回答by davidkonrad
You are not closing if
else
correct
你没有if
else
正确关闭
if (lengte > 300 || lengte < 0.4){
lengte = prompt("Geef je lengte in in kilo's");
else if (gewicht > 500 || gewicht < 0){
should be
应该
if (lengte > 300 || lengte < 0.4){
lengte = prompt("Geef je lengte in in kilo's");
} else if (gewicht > 500 || gewicht < 0){
^ <-- you lack closing of `if`
回答by qwertmax
if (lengte > 300 || lengte < 0.4){
lengte = prompt("Geef je lengte in in kilo's");
you forgeot } after if
你忘记了}之后如果
fixed
固定的
if (lengte > 300 || lengte < 0.4){
lengte = prompt("Geef je lengte in in kilo's");
} // <---
回答by anonymous
you are not closing your if statements even your if else statement below.. must use this code.
您没有关闭 if 语句,即使是下面的 if else 语句.. 必须使用此代码。
<html>
<head>
<title>
BMI calculator
</title>
</head>
<body>
<script language="JavaScript">
var leeftijd= prompt("Ben je 18 of ouder? (Ja=1)");
if (leeftijd == 1){
var gewicht= prompt("Geef je gewicht in in kilo's");
var lengte= prompt("Geef je lengte in in centimeters");
while (gewicht > 500 || gewicht < 0 || lengte > 300 || lengte < 0.4){
if (lengte > 300 || lengte < 0.4){
lengte = prompt("Geef je lengte in in kilo's");
}
else if (gewicht > 500 || gewicht < 0){
gewicht = prompt("Geef je gewicht in in kilo's");
}
}
}
var bmi = Math.round((gewicht / 100) / (lengte * lengte));
if (bmi >40)
confirm("Uw bmi is" + bmi + ". U lijdt aan extreme obesitas.");
else if (bmi > 30 && bmi <=40)
confirm("Uw bmi is" + bmi + ". U lijdt aan obesitas.");
else if (bmi > 25 && bmi <=30)
confirm("Uw bmi is" + bmi + ". U lijdt aan overgewicht.");
else if (bmi > 18 && bmi <=25)
confirm("Uw bmi is" + bmi + ". U heeft een normale BMI.");
else if (bmi < 18)
confirm("Uw bmi is" + bmi + ". U lijdt aan ondergewicht.");
else
confirm("Je moet 18 of ouder zijn om je BMI te kunnen berekenen.")
</script>
</body>
</html>