DEV Community

Discussion on: Daily Challenge #76 - Bingo! (or not...)

Collapse
 
spetastian profile image
Sebastian

JavaScript version:

const winSequence = [2, 9, 14, 7, 15];
function bingo(input = []){
 return [...new Set(input)].filter(value => winSequence.includes(value)).length === winSequence.length ? "WIN" : "LOSE";
}
Enter fullscreen mode Exit fullscreen mode

Explanation:
The Set object lets you store unique values of any type.
That is why new Set([1,2,1,3,3,4]) will give a set of 1,2,3,4. Since a set is iterable whe can spread it into an new array, thus creating a new array containing only the unique values of the input array.

const uniqueValues = [...new Set(input)] can be written as follows:

const inputSet = new Set(input);
const uniqueValuesArray = [...inputSet]
Enter fullscreen mode Exit fullscreen mode

The unique input values are then filtered with .filter()checking if each and every value is in the winning sequence using winSequence.includes(value).

This will result in an intersection between the unique values in the input array and the winning sequence. Then if the length of that intersection is the same as the length of the winning sequence, that means a "WIN".