JavaScript:未捕获语法错误:else if 行上出现意外标记

标签 javascript if-statement token

这几乎是我的第一个 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/

相关文章:

javascript - 带标签的 Css 文本输入效果

javascript - 如何在不使用 Javascript 调用它的情况下将参数传递给引用的函数

c++ - 条件内的指针语法

ios - 如何检测数字是递减还是递增? iOS

python - python中 boolean 语句和if语句的问题

security - 服务器端处理 JWT token 的最佳实践

javascript - 如何将Python创建的JSON数据发送到JavaScript?

angular - node_modules/@auth0/angular-jwt/lib/jwtoptions.token.d.ts(2,50) : error TS2304: Cannot find name 'unknown' 中的错误

c - 词法分析器: how to identify the end of a token

javascript - Google Calendar API - 如何使用 JavaScript 创建快速添加事件?