Workshop › Blackjack › Step 14 of 22

Who wins?

What we are doing

finish ends the round: it compares the hands, writes a message and pays you. Over 21 is a bust and loses at once. Otherwise the higher total wins; equal totals are a "push" and you get your bet back. A win pays the bet twice (your bet back plus as much again). A hit that makes 21 stands for you, since nothing can beat it.

```js
let message

  if (handValue(player) > 21) finish()
  else if (handValue(player) === 21) stand()

// Compare the hands and pay: a win returns the bet twice, blackjack two and a half times, a push gives it back.
function finish() {
  phase = 'done'
  const p = handValue(player)
  const d = handValue(dealer)
  let paid = 0
  if (p > 21) message = 'Bust!'
  else if (d > 21) [message, paid] = ['Dealer busts, you win', bet * 2]
  else if (p > d) [message, paid] = ['You win', bet * 2]
  else if (p < d) message = 'Dealer wins'
  else [message, paid] = ['Push', bet]
  bank += paid

  message = ''

  if (message) {
    ctx.fillStyle = '#fde047'
    ctx.font = 'bold 22px sans-serif'
    ctx.textAlign = 'center'
    ctx.fillText(message, canvas.width / 2, 212)
  }
```

What it means

Your turn

  1. Under phase, write message; in deal, clear it before phase.
  2. In hit, finish on a bust and stand on 21.
  3. Above reset, write finish with its comment.
  4. In draw, show the message under the chips.

Skills: Game states

All steps

  1. Cards
  2. A deck
  3. Shuffle
  4. Deal
  5. Cards on the table
  6. Card faces
  7. Keep drawing
  8. What is a hand worth?
  9. Aces: 11 or 1
  10. Hit: one more card
  11. Stand
  12. The hidden card
  13. Chips
  14. Who wins?
  15. The dealer's turn
  16. The next round
  17. Blackjack!
  18. Your record
  19. Double down
  20. Long hands fit
  21. Buttons
  22. Build it yourself: six decks