Mikke.Learns

Coding Project: Bingo. Part 5

May 9th, 2019

Now we know that all the numbers are drawn without errors and that the player board clears when a matching number is drawn. It’s time to move on.

I want to see if we can figure out how to check for Bingo. I’ll start with the rows. I believe those are easiest.

We want to check every value in the playerBoard object, to see if they all are zeros, or if they contain a number not yet drawn. So what if we iterate through the values in the object, and do something to them? That sounds like a forEach method!

We can skip the “for in” loop by using Object.entries. This method returns an array of the objects property values.

Object.entries(playerBoard).forEach(function([key, value]) { console.log(key + ' ' + value) });

The above code will display the playerBoards key and values in the console.

bingo-part5-1

Now that we have access to the rows of the player board, let’s check if they are all zeros. That’s where “.every” comes in. I first created another function that we can pass into “.every”.

function isZero(currentValue) { return currentValue === 0; }

It’s a simple boolean that returns true or false. When we use this together with the “.every” method, we get true only if every number in the row is a zero. It seems to be working quite nicely.

bingo-part-5-2

Here we have one row with zeros and four with higher numbers remaining. Now comes the really difficult part. Checking for Bingo in columns and diagonally.