Using OR operator in a jquery if statement

后端 未结 4 1335
悲&欢浪女
悲&欢浪女 2021-01-03 00:58

I need to use the OR operator in a jQuery if statement to filter out 10 states. My code works wile only excluding one state, but fails when i try to include multiple states.

4条回答
  •  情深已故
    2021-01-03 01:25

    Think about what

    if ((state != 10) || (state != 15) || (state != 19) || (state != 22) || (state != 33) || (state != 39) || (state != 47) || (state != 48) || (state != 49) || (state != 51))
    

    means. || means "or." The negation of this is (by DeMorgan's Laws):

    state == 10 && state == 15 && state == 19...
    

    In other words, the only way that this could be false if if a state equals 10, 15, and 19 (and the rest of the numbers in your or statement) at the same time, which is impossible.

    Thus, this statement will always be true. State 15 will never equal state 10, for example, so it's always true that state will either not equal 10 or not equal 15.

    Change || to &&.

    Also, in most languages, the following:

    if (x) {
      return true;
    }
    else {
      return false;
    }
    

    is not necessary. In this case, the method returns true exactly when x is true and false exactly when x is false. You can just do:

    return x;
    

提交回复
热议问题