0

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

3 Answers 3

4

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`
Sign up to request clarification or add additional context in comments.

Comments

3
        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");
        } // <---

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.