Workshop › 2048 › Step 8 of 26
addTile picks one empty cell at random and puts a 2 there nine times out of ten, a 4 otherwise.
```js
function addTile() {
const cells = emptyCells()
if (cells.length === 0) return
const [row, col] = cells[Math.floor(Math.random() * cells.length)]
board[row][col] = Math.random() < 0.9 ? 2 : 4
}
```Math.floor(Math.random() * cells.length) is a random position in the list, from 0 to length - 1.const [row, col] = ... takes the pair apart into two names (array destructuring).Math.random() < 0.9 is true 9 times in 10.Write addTile above function newGame() { (under emptyCells), with an empty line after it.
Skills: Arrays and objects
← Find the empty cells Two tiles to start →