Workshop › Simon Says Colors › Step 5 of 27
A forEach loop draws every pad from the list. Pad i is in column i % 2 and row Math.floor(i / 2) of a 2×2 grid,
which gives the corner of its quarter.
```js
PADS.forEach((pad, i) => {
const x = (i % 2) * HALF
const y = TOP + Math.floor(i / 2) * HALF
ctx.fillStyle = pad.dim
ctx.fillRect(x + 6, y + 6, HALF - 12, HALF - 12)
})
```PADS.forEach((pad, i) => { ... }) runs the small function once per pad: pad is the object, i its number.i % 2 is the remainder after dividing by 2: 0 for the left column, 1 for the right.Math.floor(i / 2) rounds down: 0 for the top row, 1 for the bottom.x, y are the quarter's corner; the square is drawn 6 pixels in, as before.Replace the two green lines at the end with the forEach block. Press Run.
Skills: Arrays and objects, Loops
← Two colors for every pad A function that draws →