Workshop › Pool › Step 5 of 33
Ten numbered balls packed in a triangle, the rack: row 0 has 1 ball, row 1 has 2, up to 4 in the back row. Two loops place them, each with its own color and number.
```js
const COLORS = ['#facc15', '#2563eb', '#dc2626', '#7c3aed', '#f97316', '#16a34a', '#7f1d1d', '#111827', '#0891b2', '#db2777']
// Ten balls in a triangle pointing at the cue ball: 1, 2, 3, then 4 in the back row.
function rack() {
cue = ball(CUE_START.x, CUE_START.y, '#f8fafc', 0)
balls = [cue]
let n = 0
for (let row = 0; row < 4; row++) {
for (let i = 0; i <= row; i++) {
const x = 330 + row * (R * 2 * 0.87 + 0.5)
const y = 160 + (i - row / 2) * (R * 2 + 0.5)
balls.push(ball(x, y, COLORS[n], n + 1))
n += 1
}
}
}
```row goes 0 to 3; in each row i goes 0 to row: 1 + 2 + 3 + 4 = 10 balls. n counts them for color and number.2R), centered on the middle line with i - row / 2.2R × 0.87 (√3 / 2). The extra 0.5 keeps the balls from exactly touching.R line write COLORS.function rack() {.rack, under balls = [cue], write the counter and the two loops. Press Run.Skills: Loops, Arrays and objects
← Draw the balls Numbers on the balls →