Workshop › Blackjack › Step 14 of 22
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)
}
```[message, paid] = ['You win', bet * 2] sets two variables in one line.phase, write message; in deal, clear it before phase.hit, finish on a bust and stand on 21.reset, write finish with its comment.draw, show the message under the chips.Skills: Game states