admin管理员组文章数量:1431720
I'm writing a multiple if statement in Javascript. I've 3 (or more) conditions, and I wanna doSomething() only if all these 3 conditions are true. If only one of these 3 are false, I wanna doSomethingElse(). I think my code it's right, but my problem is on another level. What if I wanna know for which condition my statement is false? E.g.: condition1=true, condition2=true, condition3=false.
if (condition1 && condition2 && condition3) {
doSomething();
} else {
doSomethingElse();
};
I've thought that I can put another if statement in the else part.
if (condition1 && condition2 && condition3) {
doSomething();
} else {
if (condition1 == false) {
doWhatIDoWhenC1isFalse();
};
};
Is this the right way? Is there any other way to do this? Maybe faster way! Thank for your help, and be nice to me, it's my second day on Javascript :)
I'm writing a multiple if statement in Javascript. I've 3 (or more) conditions, and I wanna doSomething() only if all these 3 conditions are true. If only one of these 3 are false, I wanna doSomethingElse(). I think my code it's right, but my problem is on another level. What if I wanna know for which condition my statement is false? E.g.: condition1=true, condition2=true, condition3=false.
if (condition1 && condition2 && condition3) {
doSomething();
} else {
doSomethingElse();
};
I've thought that I can put another if statement in the else part.
if (condition1 && condition2 && condition3) {
doSomething();
} else {
if (condition1 == false) {
doWhatIDoWhenC1isFalse();
};
};
Is this the right way? Is there any other way to do this? Maybe faster way! Thank for your help, and be nice to me, it's my second day on Javascript :)
Share Improve this question edited Jan 15, 2015 at 16:52 Jivings 23.3k7 gold badges62 silver badges102 bronze badges asked Jan 15, 2015 at 16:43 eneene 751 silver badge11 bronze badges 13- Sorry, It's little unclear... You want to use a function if cond1,cond2 are true and cond3 is false otherwise use another function? – Bhojendra Rauniyar Commented Jan 15, 2015 at 16:45
- 1 Yes, you are right, you will have to test for the "interesting" condition manually. There is no other way to know. – syazdani Commented Jan 15, 2015 at 16:46
- you could create an array with al the "conditions" and with that array take out only the ones that are false, but if you are talking of 3 or so conditions the way that you expressed it is the fastest... – Shocklo Commented Jan 15, 2015 at 16:48
-
if all conditions are false you execute
doWhatIDoWhenC1isFalse
while you want to know if only one is false – Fabrizio Calderan Commented Jan 15, 2015 at 16:48 -
You can try....
elseif (condition1 + condition2 + condition3 == 2)
it should work but... not very logical – Dave Goten Commented Jan 15, 2015 at 16:48
4 Answers
Reset to default 2Since the conditions are mutually exclusive, you can just use an else if
without nesting.
if (condition1 && condition2 && condition3) {
doSomething();
} else if (!condition1) {
doWhatIDoWhenC1isFalse();
}
// add more else-if conditions as needed
If only one of your conditions can be false at a time (or if you don't care when two of them are false) then you can just have three else-if clauses and check each condition individually. If you do need to treat the cases where two conditions are false separately, you'll need an else-if for each bination. Pay close attention to the order you list them in if that's the case. The cases where you check if two conditions are both false should e before the cases where you only check one condition.
if (condition1 && condition2 && condition3) {
doSomething();
}else if (!condition1){
doWhatIDoWhenC1isFalse();
}else if (!condition2){
doWhatIDoWhenC2isFalse();
}else{
doWhatIDoWhenC3isFalse();
}
You have to do something along the lines of this. No way to cleanly get which expression that failed.
You may go this way if you want if any one of the condition is false:
if ((!condition1 && condition2 && condition3)||
(condition1 && !condition2 && condition3)||
(condition1 && condition2 && !condition3))
{
doSomethingElse();
} else {
doSomething();
};
It can be:
var x = []; /* an array that will take expressions number that result true */
if(condition1) x.push(1);
if(condition2) x.push(2);
if(condition3) x.push(3);
if( x.length == 2 ){ /* if two conditions got true */
doThingForTwoTrue();
}
if( x.length == 3 ){ /* if three conditions got true */
doThingForThreeTrue();
}
if( x.indexOf(1) !== -1 ){ /* if condition1 got true */
doThingOne();
}
本文标签: javascriptWhat condition is false in a multiple conditions if statementStack Overflow
版权声明:本文标题:javascript - What condition is false in a multiple conditions if statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745574084a2664222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论