The code is not complete in the question, but it seems deck
is a global variable, which you also pass as argument to fullShuffle
. The issue is that deck.splice
empties that array completely in one call of shuffle
, so that if you use that deck
again and it gets passed via fullShuffle
to shuffle
again, you are passing an empty array and array[randomPick]
will be undefined for all 52 iterations (as array
is the same reference as deck
if my assumption about the rest of your is right).
If you want to implement your shuffle this way, then:
- Don't use a global variable inside that function
- Rely only on the argument that the function gets
- Take a copy of that array, so that you can happily splice without affecting the array of the caller
- Return the shuffled array to the caller
Code:
function shuffle(array) {
let newDeck = [];
array = [...array]; // take copy!
for (i=52; i>0; i--) {
let randomPick = Math.floor((Math.random() * array.length));
newDeck.push(array[randomPick]);
array.splice(randomPick, 1);
}
console.log(newDeck);
return newDeck; // return it.
}
function fullShuffle(cards) {
cards = shuffle(cards); // capture the returned, shuffled array
cards = shuffle(cards);
return cards; // return to caller
}
// can be called like this:
var deck = [ "ac", "2c", "3c", "4c", "5c", "6c", "7c", "8c", "9c", "10c", "jc", "qc", "kc", "ad", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "10d", "jd", "qd", "kd", "ah", "2h", "3h", "4h", "5h", "6h", "7h", "8h", "9h", "10h", "jh", "qh", "kh", "as", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s", "10s", "js", "qs", "ks" ];
deck = fullShuffle(deck);
Although for a human shuffle it makes sense to shuffle the same deck twice, for a function like this, there is little sense in calling it twice. It is like you don't trust the function to shuffle randomly enough...
Note also that there are better implementations of shuffle
. Have a look here where you find implementations of the Durstenfeld algorithm.