Plain Javascript: How to check if 1) array A has any elements of array B and 2) if array A has any elements array B doesn't have

Viewed 44

The things I want to check:

  1. Does array A have any elements of array B?
  2. Does array A have any elements that array B doesn't have?

Scenario 1:

Array A = [1, 2]

Array B = [1, 2, 3, 4]

  1. Yes
  2. No

-> Give the answer true

Scenario 2:

Array A = [1, 2, 5]

Array B = [1, 2, 3, 4]

  1. Yes
  2. Yes

-> Give answer false

Scenario 3:

Array A = [5]

Array B = [1, 2, 3, 4]

  1. No
  2. Yes

-> Give answer false

I understand it may seem/be cocky to type basically a request for some code and expect someone to write it up, but I've spent 15 hours trying to either learn enough about arrays to do it myself, and also tried different solutions that were kind of close to what I asked on stackoverflow and tweaking them, but I just can't make a system that works by answering question 1) and 2) the way I want

1 Answers
  1. A.some((x) => B.includes(x))

  2. A.some((x) => !B.includes(x))

Related