Workshop › Snake › Step 17 of 28
Random food can land on the snake's body. We keep picking until the cell is free.
```js
function placeFood() {
do {
food = { x: Math.floor(Math.random() * COLS), y: Math.floor(Math.random() * ROWS) }
} while (snake.some((part) => part.x === food.x && part.y === food.y))
}
```do { ... } while (condition) runs the body once, then again as long as the condition is true.snake.some(test) is true if at least one part passes the test: here, "is this part on the food cell?".Wrap the line inside placeFood in do { ... } while (...) as shown.
Skills: Arrays and objects, Loops
← Eat and grow A table of turns →