-4

Why doesn't this work:

if (x != (a || b || c)) {
  doStuff();
}

It's meant to check whether x is NOT equal to a OR b OR c.

EDIT: How would I achieve a check for whether x is NOT equal to a OR b OR c?

EDIT: Ok, it's a duplicate. What do I do now, take minus points even after realizing my mistake? :P

  • 2
    It should be `if(x !== a || x !== b || x !== c)`. What your expression means is, value can be either `a`, `b` or `c` and this value should not be equal to `x`. As an alternate, you can try `[a,b,c].indexOf(x) < 0` – Rajesh Oct 21 '17 at 15:15
  • `(a || b || c)` will return first truthy variable and is evaluated before `!=` – charlietfl Oct 21 '17 at 15:16
  • `[a,b,c].indexOf(x) < 0` done :D – Niet the Dark Absol Oct 21 '17 at 15:17
  • In addition to @charlietfl comment, refer this: https://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript. Any value not listed here will be truthy – Rajesh Oct 21 '17 at 15:22
  • Thank you all, especially Rajesh. I'm using this for if ((mapcount[playery - 1][playerx]) !== a) {}, so it's a part of an array based 2d game - whether a value in an array is something that you can move to or not. I'd like to have avoided doing (x !== a || x !== b .... n) becase well... It's gonna be a huge list of stuff with 15 different "don't move over this" values to be checked. Just one after another would have been great :P But it doesn't work that way, too bad. And yes, I'm green like beansprouts on a bright spring day. – Jaakko Pöntinen Oct 21 '17 at 15:42
  • If you want to see: https://codepen.io/jpontinen/pen/MERwQr?editors=0010 – Jaakko Pöntinen Oct 21 '17 at 15:43

1 Answers1

-2

To use multiples values like you wanna just:

 
var x = 'x';
var a = 'a';
var b = 'b';
var c = 'c';

function doStuff() {
  console.log(1)
}

// exemple 1
if(x == a || x == b || x == c) {
   doStuff();
}

function isSameValue(element, index, array) {
  return element === x;
}

// exemple 2
if([a, b, c].some(isSameValue)) {
  doStuff();
}

// exemple 3
[a, b, c].includes(x);