简体   繁体   中英

JavaScript: Uncaught SyntaxError: Unexpected token on an else if line

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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