0

Hi I want to know is there a better way to do this? condition_a is more important than b, b is more important than c, etc.

var slot = condition_a

if (!slot) {
  slot = condition_b
  if (!slot) {
    slot = condition_c
    if (!slot) {
      slot = condition_d
      if (!slot) {
        slot = condition_e
      }
    }
  }
}

if (slot) {
  //do something
}
lock
  • 13
  • 1
  • There is no better way for this. whatever you did is correct way. – Jaydip Jadhav Jan 06 '16 at 11:52
  • No, I am 25. Just learning a hobby. – lock Jan 06 '16 at 11:53
  • 1
    @JaydipJadhav - there's 3 answers that are far more succinct, and, if you know javascript, are far more readable to even an intermediate level coder – Jaromanda X Jan 06 '16 at 11:54
  • Possible duplicate of http://stackoverflow.com/questions/2922948/javascript-switch-vs-if-else-if-else? The linked article on that question has a rather good (but possibly outdated) breakdown of conditional performance. – danShumway Jan 06 '16 at 13:07

3 Answers3

3

You do a OR:

if (condition_a || condition_b || condition_c || condition_d || condition_e) {
      // do something
}

This is equivalent to your code. If a condition evaluates to true, the following ones aren't checked (it's called short circuit evaluation). It makes it possible to have checks like this:

if (a===null || a.b===0) {

Be careful that so many conditions at the same place look like a code smell: there's probably something which hasn't be designed semantically enough.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
  • Not sure why this got a downvote, this seems to be a perfectly valid answer. – TMH Jan 06 '16 at 11:47
  • I didn't downvoted, but I think he wants `if (!condition_a || !condition_b || ... !condition_e)` – gurvinder372 Jan 06 '16 at 11:48
  • 1
    @JaromandaX read OP again, he is checking negative of `slot` not `slot` itself. – gurvinder372 Jan 06 '16 at 11:50
  • @gurvinder372 - brush up on your logic – Jaromanda X Jan 06 '16 at 11:51
  • @gurvinder372 He is checking negative of slot just to see if another condition needs to be checked – Denys Séguret Jan 06 '16 at 11:51
  • @gurvinder372 but the logic quiz results in that solution ;) – CoderPi Jan 06 '16 at 11:51
  • DenysSéguret, agreed my bad +1 for the good logic. @Jaromand-x perhaps you can brush up your articulation skills. An explanation would have suffice than saying `wrong`!! – gurvinder372 Jan 06 '16 at 11:57
  • @gurvinder372 he just gave you the change to solve the quiz yourself – CoderPi Jan 06 '16 at 12:06
  • @gurvinder372 - why? you didn't ask the question. You merely added what you "think" the OP wanted, you added nothing to this answer except confusion - anything more than `wrong` would appear as if I cared – Jaromanda X Jan 06 '16 at 12:09
  • @Denys Séguret Sorry but that doesn't work, I should have made it more clear in my question. The condition_ are different actions, so the //do something part will be different base on the slot. I want to check if condition_a is true, if true do something immediately. If failed then check condition_b if true do something immediately – lock Jan 06 '16 at 12:15
  • @lock This is exactly what's achieved. The second part of the || is only executed when the first part is falsy. – Denys Séguret Jan 06 '16 at 12:17
  • @Denys Séguret I just test it and it worked! This is so much more convenient. – lock Jan 06 '16 at 12:36
  • @lock - how can this possibly work since you don't know WHICH condition's function to use? – Jaromanda X Jan 06 '16 at 12:52
  • @Jaromanda X I am using your variation. By worked I mean the Short-Circuit Evaluation. – lock Jan 06 '16 at 13:22
0

You can use || condition like this also,

slot = condition_a||condition_b||condition_c||condition_d;

Example Fiddle

ssilas777
  • 9,166
  • 3
  • 41
  • 63
-1

if you need slot to have a value (and not just using it to test the condition at the end)

var slot = condition_a || condition_b || condition_c || condition_d || condition_e;
if (slot) {
  // do soemthing
}
Jaromanda X
  • 47,382
  • 4
  • 58
  • 76