Skip to content

Instantly share code, notes, and snippets.

@martypdx
Last active August 29, 2015 14:17
Show Gist options
  • Save martypdx/e2d54f2098160bc68b9d to your computer and use it in GitHub Desktop.
Save martypdx/e2d54f2098160bc68b9d to your computer and use it in GitHub Desktop.
model a deck of cards
function Card( suit, rank ) {
this.suit = suit;
this.rank = rank;
this.toHtml = function() {
return '<li>' + this.rank + ' of ' + this.suit + '</li>';
}
}
function Deck( suits, ranks ) {
this.cards = [];
var card, s, suit, r, rank;
for ( s = 0; s < suits.length; s++ ) {
suit = suits[s];
for ( r = 0; r < ranks.length; r++ ) {
rank = ranks[r];
card = new Card( suit, rank );
this.cards.push( card );
}
}
this.toHtml = function() {
var html = '<ul>';
for ( var c = 0; c < this.cards.length; c++ ) {
html += this.cards[c].toHtml();
}
html += '</ul>';
return html;
}
}
var suits = [ 'hearts', 'diamonds', 'spades', 'clubs' ];
var ranks = [ 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king', 'ace' ];
var deck = new Deck( suits, ranks );
document.write( deck.toHtml() );
<script src='cards.js' type='text/javascript'></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment