JSFiddle - React, Tailwind, and code Playground
by Bhesh Gurung
JavaScript
var suits = ["C", "D", "H", "S"],
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
function Card(suit, rank) {
this.suit = suit;
this.rank = rank;
}
Card.prototype.toString = function() {
return this.suit + "_" + this.rank;
}
function Deck() {
var cards = [],
drawIdx;
for (var s = 0; s < suits.length; s++) {
for (var r = 0; r < ranks.length; r++) {
cards.push(new Card(suits[s], ranks[r]));
}
}
}
Deck.prototype.shuffle = function() {
drawIdx = cards.length - 1;
}
Deck.prototype.draw = function() {
if (drawIdx < 0) {
throw Error("This Deck needs a shuffle.");
}
var card = cards[drawIdx];
drawIdx--;
return cards;
}
var deck = new Deck();