Workshop › Connect Four › Step 22 of 38
lineThrough(row, col, [dx, dy]) starts at a disc and walks in one direction while the discs are the same color, then
the other way, collecting the cells. [1, 0] is across: one column right per step.
```js
// Count the discs in a row through (row, col) in one direction and its opposite.
function lineThrough(row, col, [dx, dy]) {
const who = board[row][col]
const cells = [[row, col]]
for (const sign of [1, -1]) {
let r = row + dy * sign
let c = col + dx * sign
while (inside(r, c) && board[r][c] === who) {
cells.push([r, c])
r += dy * sign
c += dx * sign
}
}
return cells
}
```[dx, dy] in the parameters names the two items of the direction array.for (const sign of [1, -1]) runs once forwards and once backwards.while repeats as long as its condition is true: while still on the board and the same color, add the cell and step on.Above function play( write the comment and lineThrough, followed by an empty line.
Skills: Loops, Arrays and objects
← Inside the board? Four in a row? →