Using OR operator in a jQuery if statement
if-statement, jquery, operators
Solution
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 is 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;
Problem
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. Is there a correct way to do this? Code I am using : ``` if ((state != 10) || (state != 15) || (state != 19) || (state != 22) || (state != 33) || (state != 39) || (state != 47) || (state != 48) || (state != 49) || (state != 51)) return true; ```