Workshop › Sudoku › Step 3 of 28
A puzzle is 81 digits, row by row, with 0 for an empty cell. We write it as one text and turn it into a 9 × 9 grid of numbers, then draw the digits.
```js
// The puzzle, row by row; 0 is an empty cell.
const PUZZLE = '530070000600195000098000060800060003400803001700020006060000280000419005000080079'
let grid // grid[r][c]: 1 to 9, or 0 for an empty cell
function reset() {
grid = Array.from({ length: 9 }, (_, r) => [...PUZZLE.slice(r * 9, r * 9 + 9)].map(Number))
}
if (grid[r][c] === 0) continue
ctx.fillStyle = '#0f172a'
ctx.font = 'bold 26px sans-serif'
ctx.textAlign = 'center'
ctx.fillText(String(grid[r][c]), x + SIZE / 2, y + SIZE / 2 + 9)
```Array.from({ length: 9 }, (_, r) => ...) builds 9 rows; row r is the 9 characters from position r * 9.[...text] splits a text into characters; .map(Number) turns '5' into 5.0) are skipped with continue; the others show their digit in the middle of the cell.TOP, write the comment, PUZZLE, let grid and reset.draw, under the white cell, write the digit lines.reset() above draw() at the bottom.Skills: Arrays and objects
← Lines and boxes The puzzle's own digits →