Workshop › Candy Crush-style Match 3 › Step 6 of 29
We fill the board row by row, left to right, so when a gem is placed only the cells to its left and above it exist. If the two to its left, or the two above it, have its color, it would make three: then we pick again.
```js
// Would this gem make three in a row with the two to its left, or the two above it?
function makesRun(r, c, gem) {
const left = c >= 2 && board[r][c - 1] === gem && board[r][c - 2] === gem
const up = r >= 2 && board[r - 1][c] === gem && board[r - 2][c] === gem
return left || up
}
// A new board with no three in a row: each gem avoids the colors that would make one.
let gem
do gem = randomGem()
while (makesRun(r, c, gem))
board[r].push(gem)
```left is true if there are two cells to the left (c >= 2) and both have this color; up the same above.do ... while (condition) runs its body at least once, then again while the condition is true: pick a gem, and
pick again while it would make a run.function newBoard() { write the comment, makesRun, and the comment for newBoard.newBoard, replace board[r].push(randomGem()) with the four lines.Skills: Loops, Functions