Workshop › Sudoku › Step 18 of 28
Now we teach the computer to solve. A human starts with the cell where the fewest digits fit, and so will the computer:
bestCell finds the empty cell with the fewest options, and lists them.
```js
// The empty cell with the fewest digits that fit, and those digits.
function bestCell(board) {
let found = null
for (let r = 0; r < 9; r++) {
for (let c = 0; c < 9; c++) {
if (board[r][c] !== 0) continue
const options = []
for (let d = 1; d <= 9; d++) if (canPlace(board, r, c, d)) options.push(d)
if (!found || options.length < found.options.length) found = { r, c, options }
}
}
return found
}
```options lists the digits that fit, using canPlace.found keeps the cell with the shortest list so far.null.Under canPlace, write the comment and bestCell.
Skills: Loops, Arrays and objects