I'm trying to randomly shuffle a deck array of Card
objects into a newDeck
array of Card
objects using array.splice()
. I imagine my problem is either something to do with variable scope, or a misapprehension about array.splice()
.
var deck = [new Card(), new Card(), new Card(), new Card(), new Card(), new Card()];
var newDeck = [];
var shuffle = function(){
var i = "";
for (i = 0; i < deck.length; i++){
newDeck[i] = deck.splice(Math.floor(Math.random() * deck.length, 1));
}
};
shuffle();
Is there a better way to shuffle a deck?