Workshop › Conway's Game of Life › Step 15 of 32
How many cells are alive? population() adds up the whole grid with reduce, and we show it at the top right.
```js
const population = () => grid.reduce((sum, row) => sum + row.reduce((a, b) => a + b, 0), 0)
ctx.fillText('Generation ' + generation, 8, 24)
ctx.textAlign = 'right'
ctx.fillText('Alive ' + population(), canvas.width - 8, 24)
}
```reduce turns a list into one value: [1, 0, 1].reduce((a, b) => a + b, 0) starts at 0 and adds each item: 2.reduce adds up one row; the outer one adds up the rows' sums.textAlign = 'right' puts the text's right end at x, 8 pixels from the right edge, so it never runs off.step, after an empty line, write population.draw, under the Generation line, write the two new lines.Skills: Arrays and objects
← A world without edges A generation every few frames →