Workshop › Tower Defense › Step 15 of 42
A tower can only go on grass, inside the map, on a free tile, and only if you can pay for it. canBuild answers all
of that in one place, and build refuses when the answer is no.
```js
function canBuild(col, row) {
const inside = col >= 0 && col < COLS && row >= 0 && row < ROWS
const taken = towers.some((t) => t.col === col && t.row === row)
return inside && !road.has(key(col, row)) && !taken && gold >= TOWERS[selected].cost
}
function build(col, row) {
if (!canBuild(col, row)) return
```inside: the column and the row are both within the map.taken: some tower already stands there (some asks "is there at least one?").true only if all four are right: && means "and", ! means "not".function build(col, row) { write canBuild.if as the first line of build.Skills: Game states, Collision detection
← Click to build Show the gold →