Skip to content

Instantly share code, notes, and snippets.

@b3kN
Created October 22, 2014 23:20
Show Gist options
  • Save b3kN/b30633d4729a473f4ae0 to your computer and use it in GitHub Desktop.
Save b3kN/b30633d4729a473f4ae0 to your computer and use it in GitHub Desktop.
Deck shuffle test
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