-1

I have an array and I need to check if that array contains 2 and 3.

For example a = [1, 3].

I can do it with

a.includes(2) && a.includes(3)

I tried the following but I get inconsistent result, I don't understand why:

a.includes(1 && 3)
// true
a.includes(1 && 2)
// false
a.includes(2 && 3)
// true
Tom Bomb
  • 1,204
  • 3
  • 10
  • 20
  • 1
    Includes() cannot accept two arguments like that. You can find out more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes – Alex May 16 '19 at 08:02

1 Answers1

1

a.includes(1 && 3) doesnot pass two arguments two the function. 1 && 3 is an expression which evaluates to the first falsy value value. If there is no falsy value the last value is returned. So 1 && 3 evaluates to 3

console.log(1 && 3) //3

You can use every()

[1,2].every(x => a.includes(x))
Maheer Ali
  • 32,967
  • 5
  • 31
  • 51