Using the Fisher–Yates formula to randomly shuffle our cards, learn how to build out the prototypes further by adding a shuffle function and a button that will shuffle the cards on demand.
- Let's change the shuffle function.…We'll use an algorithm called the Fisher-Yates shuffle.…Mike Bostock does a great job of illustrating…how this algorithm works.…I won't try and replicate it here,…but check out his site if you're interested…in learning more about it.…Here's what it looks like in code.…Let's delete the console.log(this).…And we'll say variable…cardsToShuffle = this.…Remember this is the game, so we'll say gameDeck.…
And the data for the game is in the deckData.…We'll create a few more variables.…First we'll have M, which is equal to cardsToShuffle.length.…We'll also have a T which is a temporary variable…and I for the iterator.…We're going to use a while loop here,…so while(m) which is the number of cards.…We're going to assign or iterator to a random number.…
To do that, we're first going to use math.floor.…And remember floor is like rounding,…but you round to the lower number.…And we'll say math.random which creates a random number.…We will multiply that by M--.…And M minus minus is just an iterator that goes down…
Released
2/26/2018- Working with prototype and dynamic elements
- Using bind, call, and apply
- Using the Fisher–Yates formula
- When to use e.preventDefault() and when to avoid it
- Drag-and-drop life cycle
- Using prototypical inheritance to optimize your code
Share this video
Embed this video
Video: Shuffling