Home > database >  How to check one array if has values from other arrays in Typescript
How to check one array if has values from other arrays in Typescript

Time:03-19

how can I check if winningConditions are in chosenNumbers. As you see chosenNumbers are not same length and have a mix of number, and from this mix of number we got one condition 0, 3, 6. But there is also 2. How to check to make it work. I want to get true if i.e winning condition array [0, 3, 6] has same numbers as chosen numbers [0, 2, 3, 6], excluding 2 because it is out of scope. I hope you understand what I mean.

private chosenNumbers [0, 2, 3, 6]
private winningConditions = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];

CodePudding user response:

I'm not sure this is the more efficient way to do it, but I think this function does what you're asking for:

function check(chosenNumbers, winningConditions) {
  return winningConditions.some((condition) => {
    return condition.every((element) => {
      return chosenNumbers.includes(element);
    });
  });
}
  • Related