JavaScript to Shuffle a Deck of Cards

It was just another day. I came across this brain teaser and I decided to do it in JS. Namely, a javascript function to shuffle a deck of cards. I express the number as in an array.

const leaf = 52; // count of cards
var deck = [];
let j = Math.floor( (Math.random() * leaf) % leaf);

for (i=1; i<=leaf; i++) {
  while (deck[j]) {
    j = Math.floor( (Math.random() * leaf) % leaf);
  }
  deck[j] = i;
}
console.log( deck );

Leave a comment