[英]JavaScript: Uncaught SyntaxError: Unexpected token on an else if line
this is my pretty much my first JavaScript program. 这几乎是我的第一个JavaScript程序。 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. 我不知道为什么它不起作用,我不知道如何正确调试,我在Google chrome上使用F12进入开发人员模式。 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. 如果我加载html页面,则什么也没有发生,并且控制台显示:Uncaught SyntaxError:其他意外令牌,并且该错误来自第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>
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`
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");
} // <---
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.