Workshop › 2048 › Step 3 of 26
The board's data is a 2D array: an array of 4 rows, each an array of 4 numbers; 0 is an empty cell and
board[row][col] reads one. Building it hides one of JavaScript's most famous traps.
```js
let board
function newGame() {
// Array.from calls the function for every row, so each row is its own array.
board = Array.from({ length: SIZE }, () => Array(SIZE).fill(0))
}
newGame()
draw()
```Array(SIZE).fill(0) is one row of four zeros.Array.from({ length: SIZE }, () => ...) calls the arrow function once per row, so every row is a new array.Array(4).fill(Array(4).fill(0)) puts the same row object in all four places. Setting
board[0][0] = 2 would change board[1][0], board[2][0] and board[3][0] too, since they are one row.newGame() builds the board before the first draw().TOP, after an empty line, write let board.function cellX(col) { write newGame, with an empty line after it.newGame() above draw().Skills: Arrays and objects
← Sixteen cells A color for every number →