我一直在研究原始代码并得到了意想不到的结果。
var one = prompt("Enter the number", "4");
var age = parseInt(one);
if (age >= 14 || age <= 90) {
console.log("Correct");
} else {
console.log("Wrong")
}
例如,当我输入 100 时,它会显示“正确”而不是“错误”。
您能否回答一下为什么它会以这种方式工作?
最佳答案
您正在使用 or 运算,因此当 age
为 100 时,OR 运算的第一部分为 true,这意味着整个 OR 条件为 true,因为 true OR false
是真的。
您需要使用和AND
运算符
var one = prompt("Enter the number", "4");
var age = parseInt(one);
if (age >= 14 && age <= 90) {
console.log("Correct");
} else {
console.log("Wrong")
}
关于javascript - 请帮助找出为什么代码以这种方式工作(JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28899493/