Workshop › Memory › Step 9 of 25
Now the drawing reads the cards instead of loop counters: a function draw paints the table, then each card at its
own place. The nested loops are no longer needed.
```js
function draw() {
ctx.fillStyle = '#1e1b4b'
ctx.fillRect(0, 0, canvas.width, canvas.height)
for (const card of cards) {
const x = cardX(card)
const y = cardY(card)
ctx.fillStyle = '#6366f1'
ctx.fillRect(x, y, CARD, CARD)
}
}
newGame()
draw()
```for (const card of cards) runs once for each card, calling it card.x and y come from cardX and cardY.newGame() builds the cards first, then draw() paints them.Replace the loose drawing lines (background and loops) with the draw function, and call draw() after newGame().
Skills: Functions, Loops
← A card's corner Draw again and again →