所以我正在用 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/