如果法规运行javascript,为什么还有其他人

时间:2017-08-19 01:19:23

标签: javascript if-statement execution

所以我正在研究JavaScript中的贷款计算器。所以我一直遇到这个问题,当用户输入一个数字超过400但低于500时,它将不会运行if语句,而是运行另一个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
    }
}

2 个答案:

答案 0 :(得分:3)

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:

&#13;
&#13;
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
&#13;
&#13;
&#13;

答案 1 :(得分:1)

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 (getCredit > 500 && getCredit <= 640){
        alert("I learned something at Mathnasium");
        score += 6
        console.log("The score is " + score);
    }

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