Mikke.Learns

# Coding Project: Bingo. Part 6

May 12th, 2019

type: entry-hyperlink id: c2nIviTpetRCAQFtoXXWrgq Since then I’ve been struggling to check the columns. There has been a lot of trial and error, but it seems that I’ve come up with a solution.

First I created a function for checking the columns.

`function checkForBingoVertical() {}`

Since we’re looking for zeros in every column, I thought that I could use the first row as a trigger. If there is a zero in the B-row, we should check the rest of the column, if they are also zeros.

```for (i = 0; i < playerBoard.b.length; i++) { if (playerBoard.b[i] === 0) {} };```

If the for loop finds a zero I again check every entry in the player board. But this time I’ve limited it to the column in which we found a zero. So if the second number in the B-row is a zero, we would check the following rows at that same index (array index 1).

```Object.entries(playerBoard).forEach(function([key, value]) { if (isZero(value[i])) { counter += 1; }; });```

If there is a match (a zero), we increase the counter variable. At the end of the if statement we check to see if the counter is less then five.

```if (counter < 5) { counter = 0; } else if (counter == 5) { console.log('BINGO') };```

If so, we reset the counter since we don’t have Bingo. If the counter is 5, we have five zeros in a row, and we’ve got Bingo and the game should end.

This is how the entire function looks:

```function checkForBingoVertical() { let counter = 0; for (i = 0; i < playerBoard.b.length; i++) { if (playerBoard.b[i] === 0) { Object.entries(playerBoard).forEach(function([key, value]) { if (isZero(value[i])) { counter += 1; }; }); if (counter < 5) { counter = 0; } else if (counter == 5) { console.log('BINGO') }; }; } };```

EDIT: As I was writing this post, I realized that we don’t need to first check the B-row for a zero. We’re checking every row in the Object.entries method anyway. So I deleted it, and sure enough, it still works! It’s great to be able to shorten the code.

This is the new function:

```function checkForBingoVertical() { let counter = 0; for (i = 0; i < playerBoard.b.length; i++) { Object.entries(playerBoard).forEach(function([key, value]) { if (isZero(value[i])) { counter += 1; }; }); if (counter < 5) { counter = 0; } else if (counter == 5) { console.log('BINGO') }; } };```