Shuffling rearranges a collection into a random order. For a card deck stored as a Set, first copy its values into an array because Sets have insertion order but no indexes. section 26.1
The shuffle operation proceeds in a short sequence:
- Spread the Set into an array. section 26.1
- Visit each array index and choose a random swap index with Math.random(). section 26.1
- Swap the cards, clear the original Set, and add the cards back in their new order. section 26.1
Shuffling a blackjack deck
createDeck builds a Set from every suit-and-face combination, calls shuffle(deck), and returns the rearranged deck ready for dealing. section 26.1
Listing 26.6. src/cards.js
Inspect the shuffle implementation, including the loop, random swap, deck clearing, and reinsertion steps. section 26.1


