Workshop › Chess › Step 24 of 48
A pawn that reaches the far side is promoted to a queen. The move carries an extra field promo: 'Q', so add
learns to take extra fields, and play puts a queen on the board instead of the pawn.
```js
const add = (r, c, tr, tc, extra = {}) => moves.push({ from: [r, c], to: [tr, tc], ...extra })
const dir = color === 'w' ? -1 : 1
const last = color === 'w' ? 0 : 7
const promote = (tr) => (tr === last ? { promo: 'Q' } : {})
if (!board[r + dir][c]) {
add(r, c, r + dir, c, promote(r + dir))
if (board[tr][tc] && board[tr][tc][0] !== color) add(r, c, tr, tc, promote(tr))
board[tr][tc] = m.promo ? piece[0] + m.promo : piece
```extra = {} is a default value: if add is called without a fifth argument, extra is an empty object.
...extra copies its fields into the move.promote(tr) returns { promo: 'Q' } when the pawn lands on the last row, otherwise {}.play, piece[0] + m.promo makes 'wQ' from 'wP'.pseudoMoves, change the add line.last and promote under dir, and pass promote(...) to the two add calls shown.play, change the board[tr][tc] = piece line.Skills: Functions, Game states
← Pawns capture diagonally Is a square attacked? →