这几乎是我的第一个 JavaScript 程序。 我不明白为什么它不起作用,我不知道如何正确调试,我在 Google chrome 上使用 F12 进入开发人员模式。如果我加载 html 页面,则不会发生任何事情,控制台会显示: 未捕获的语法错误:意外的标记,并且错误来自第 18 行。
这是我的整个代码,因为问题可能不仅仅出在第 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>
最佳答案
您不会关闭if
else
正确
if (lengte > 300 || lengte < 0.4){
lengte = prompt("Geef je lengte in in kilo's");
else if (gewicht > 500 || gewicht < 0){
应该是
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`
关于JavaScript:未捕获语法错误:else if 行上出现意外标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18967582/