Created
October 22, 2014 23:20
-
-
Save b3kN/b30633d4729a473f4ae0 to your computer and use it in GitHub Desktop.
Deck shuffle test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var suits = ['S', 'H', 'D', 'C'], | |
numbers = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'], | |
deck = []; | |
// Loop to organize by suit | |
for (var i = 0; i < suits.length; i++) { | |
// Loop to add number to suit | |
for (var x = 0; x < numbers.length; x++) { | |
// Push suit + number to deck array | |
deck.push(suits[i] + numbers[x]); | |
} | |
} | |
// Function to shuffle newly created deck | |
function shuffleDeck(n) { | |
// Array for shuffled deck | |
var shuffled = []; | |
// Loop for cards in deck | |
for (var y = 0; y < deck.length; y++) { | |
// Selecting random card | |
var rand = Math.floor(Math.random() * (y + 1)); | |
// Setting cards in shuffled deck | |
shuffled[y] = shuffled[rand]; | |
shuffled[rand] = deck[y]; | |
} | |
console.log(shuffled); | |
} | |
shuffleDeck(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment