Workshop › Blackjack › Step 3 of 22

Shuffle

What we are doing

A sorted deck is no game. The Fisher–Yates shuffle is the fair way: from the last card down, swap each card with a random card at or before it.

```js
// A new shuffled deck of 52 cards (Fisher–Yates).
  for (let i = deck.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1))
    ;[deck[i], deck[j]] = [deck[j], deck[i]]
  }
```

What it means

Your turn

  1. Above newDeck, write the comment.
  2. At the end of newDeck, write the shuffle loop.

Skills: Arrays and objects, Loops

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