最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What condition is false in a multiple conditions if statement - Stack Overflow

programmeradmin1浏览0评论

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
 |  Show 8 more ments

4 Answers 4

Reset to default 2

Since 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();
}
发布评论

评论列表(0)

  1. 暂无评论