8

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;
Syscall
  • 19,327
  • 10
  • 37
  • 52
Ledattack
  • 93
  • 1
  • 1
  • 5
  • 5
    you probably mean and? – aw04 Oct 24 '16 at 15:24
  • @aw04 Yes! using the AND operator worked. I should have tried this before i posted this question. The other answers were good to know, but this was the easiest possible solution. Thanks – Ledattack Oct 24 '16 at 20:24
  • Does this answer your question? [Why does non-equality check of one variable against many values always return true?](https://stackoverflow.com/questions/26337003/why-does-non-equality-check-of-one-variable-against-many-values-always-return-tr) – khelwood Jan 14 '22 at 20:02

4 Answers4

33

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;
5

The code you wrote will always return true because state cannot be both 10 and 15 for the statement to be false. if ((state != 10) && (state != 15).... AND is what you need not OR.

Use $.inArray instead. This returns the index of the element in the array.

JSFIDDLE DEMO

var statesArray = [10, 15, 19]; // list out all

var index = $.inArray(state, statesArray);

if(index == -1) {
    console.log("Not there in array");
    return true;

} else {
    console.log("Found it");
    return false;
}
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
  • this is a fine alternative and all, but you don't even mention the problem with the code posted... this answer will likely only confuse the OP – aw04 Oct 24 '16 at 15:42
2

Update: using .indexOf() to detect if stat value is one of arr elements

Pure JavaScript

var arr = [20,30,40,50,60,70,80,90,100];
//or detect equal to all
//var arr = [10,10,10,10,10,10,10];
    var stat = 10;

if(arr.indexOf(stat)==-1)alert("stat is not equal to one more elements of array");
Mamdouh Saeed
  • 2,302
  • 1
  • 9
  • 11
1

The logical OR '||' automatically short circuits if it meets a true condition once.

false || false || true || false = true, stops at second condition.

On the other hand, the logical AND '&&' automatically short circuits if it meets a false condition once.

false && true && true && true = false, stops at first condition.

four
  • 564
  • 4
  • 6