Workshop › 2048 › Step 7 of 26
Find the empty cells
What we are doing
New tiles appear in random empty cells. First we collect all of them as [row, col] pairs.
```js
function emptyCells() {
const cells = []
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (board[row][col] === 0) cells.push([row, col])
}
}
return cells
}
```
What it means
- The loops visit all 16 cells; each empty one is pushed as a small array
[row, col].
- The result is a list of pairs, like
[[1, 1], [3, 3]].
Your turn
Write emptyCells above function newGame() {, with an empty line after it.
Skills: Arrays and objects, Loops
← Big numbers, smaller text A new tile →
All steps
- The board
- Sixteen cells
- A grid of numbers
- A color for every number
- Numbers on the tiles
- Big numbers, smaller text
- Find the empty cells
- A new tile
- Two tiles to start
- Slide a row
- Merge equal tiles
- Move left
- The arrow key
- Only real moves count
- Right is left, reversed
- Up and down are columns
- Score
- Is there a move left?
- Game over
- Making 2048
- Win and lose screens
- Space to go on
- Best score
- Swipes
- A tap is not a swipe
- Build it yourself: undo