javascript - 为什么另一个 else if 语句运行 javascript

标签 javascript if-statement execution

所以我正在用 JavaScript 开发一个贷款计算器。所以我一直遇到这个问题,当用户输入高于 400 但低于 500 的数字时,它不会运行该 if 语句,而是会运行另一个 else if 语句,其中如果数字高于 500 且低于 640。我正在谈论的代码如下。任何帮助将不胜感激。

else if (500 < getCredit < 640){
        alert("This is it");
        score += 6
        console.log("The score is " + score);
    }

    else if (400 < getCredit < 500) {
        score += 0 // The else if statement above is executed and not this  one whenever conditions are met
    }

下面是其余代码 JS代码

    function myFunction() {

    score = 0;

    var getCredit = parseInt(document.getElementById("credit").value);
    if(getCredit > 640){
        score += 12
        console.log("The score is " + score);// This is working the right way
    }

    else if (500 < getCredit < 640){
        alert("I learned something at Mathnasium");
        score += 6
        console.log("The score is " + score);
    }

    else if (400 < getCredit < 500) {
        score += 0
    }
}

最佳答案

if (getCredit >= 640) {
    // credit is 640 or more
}
else if (getCredit >= 500) {
    // credit is 500 or more, and less than 640 because of the "if (getCredit >= 640)" above
}
else if (getCredit >= 400) {
    // credit is 400 or more, and less than 500
}
<小时/>

else if (500 < getCredit < 640){从左到右计算为

else if ( (500 < getCredit) < 640) {

评估结果为 (true < 640)(false < 640)但是两者的结果都是 true:

console.log(500 < 1 < 640)   // true   (false < 640)

console.log(true  < 640)     // true

console.log(false < 640)     // true

console.log(true  == 1)      // true 

console.log(false == 0)      // true

关于javascript - 为什么另一个 else if 语句运行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45766786/

相关文章:

KSH shell 脚本不会执行并返回 127(未找到)

javascript - JSON.parse 将字符串转换为数组错误

c# - 如何更改 Internet Explorer 中禁用的 html 控件的颜色

C# - 多个 if else 语句无法正常工作

javascript - knockout if 语法

java - Android - 优化多个if语句

eclipse - 如何设置 eclipse java 调试器中的当前执行行?

javascript - 语法错误 : identifier starts immediately after numeric literal in Firebug

javascript - 单击按钮时从数组中交换图像

hardware - 机器周期、总线周期和执行周期的区别