javascript 如果变量匹配 3 个条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8912492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If variable matches 3 conditions
提问by Alex
I need to have an if statement to check if a number is = 0, or between 5000 and 3600000. How can have it check all 3 conditions?
我需要一个 if 语句来检查一个数字是否为 = 0,或者介于 5000 和 3600000 之间。如何让它检查所有 3 个条件?
Number can be 0 or 5000 or above OR 3600000 or less. It can't be -1, 1.1 2.3 or anything between 0 and 5.
数字可以是 0 或 5000 或以上或 3600000 或以下。它不能是 -1、1.1 2.3 或 0 到 5 之间的任何值。
Here is what I have now and this work for checking if less than 5000 or greater than 3600000
这是我现在所拥有的,这项工作用于检查是否小于 5000 或大于 3600000
var intervalCheck = interval * digit
if(isNaN(intervalCheck))
{
alert("Must be a number.");
}
else if((intervalCheck < 5000)||(intervalCheck>=3600000))
{
alert("Must be between 5 seconds and 1 hour.");
}
else
{
localStorage["interval_setting"] = interval * digit;
saveActions();
}
回答by jfriend00
if ((intervalCheck == 0) || (intervalCheck >= 5000 && intervalCheck <= 3600000)) {
// put your code here
}
You can combine as many logic operations as you want into one if
statement using any of the javascript logic and comparision operators.
您可以if
使用任何 javascript 逻辑和比较运算符将任意数量的逻辑操作组合到一个语句中。
You can see a demo work here: http://jsfiddle.net/jfriend00/y5tRK/
你可以在这里看到一个演示工作:http: //jsfiddle.net/jfriend00/y5tRK/
回答by Brombomb
else if(intervalCheck === 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000)) {
localStorage["interval_setting"] = interval * digit;
saveActions();
}
else {
alert('You broke something");
}
The ===
means to check both type and value. This makes sure that intervalCheck is both a number and equal to zero, instead of evaluating to true if intervalCheck is ""
or false
. Then we give our or
condition. We wrap this in parenthesis becuase we need to ensure that the value falls between our two numbers. The parenthesis mean this will be evaluated as a whole with the or
.
的===
装置,以检查这两种类型和值。这确保 intervalCheck 既是数字又等于零,而不是在 intervalCheck 是""
或 时评估为真false
。然后我们给出我们的or
条件。我们将其括在括号中,因为我们需要确保该值介于我们的两个数字之间。括号表示这将与or
.
回答by Soufiane Hassou
I need to have an if statement to check if a number is = 0, or between 5000 and 3600000.
我需要一个 if 语句来检查一个数字是 = 0,还是在 5000 到 3600000 之间。
if( intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000) ) {
//code here
}
回答by Karl Mendes
if(intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck<=3600000))
回答by Karl Mendes
if(!isNaN(intervalCheck) && (intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000)))