Last active
August 29, 2015 14:17
-
-
Save martypdx/e2d54f2098160bc68b9d to your computer and use it in GitHub Desktop.
model a deck of cards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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