admin管理员组文章数量:1278788
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 es from line 18.
This is my whole code, seeing as the problem might not lie on line 18 alone:
<!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>
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 es from line 18.
This is my whole code, seeing as the problem might not lie on line 18 alone:
<!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>
Share
Improve this question
asked Sep 23, 2013 at 19:48
GihiGihi
171 gold badge1 silver badge5 bronze badges
1
- 1 In the future, you can easily validate your javascript online. – Bucket Commented Sep 23, 2013 at 19:59
3 Answers
Reset to default 4You are not closing if
else
correct
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.
<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 statementJavaScript Uncaught SyntaxError Unexpected token on an else if lineStack Overflow
版权声明:本文标题:if statement - JavaScript: Uncaught SyntaxError: Unexpected token on an else if line - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741299943a2371036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论