- Create a Card(suit, number)constructor with data memberssuitand number. Make it so that it is impossible to change the suit and number of a card (yes, you will need to use closures). Create the following functions for this prototype:- toString(): returns a string representation of the card, for example: AD, 4C, 2D.
- equal(otherCard): returns true if this card equals- otherCard.
- cmp(otherCard): returns -1 if the number of this card is less than otherCard, 1 if it is greater, and 0 if they are the same.
 
- Create Deckconstructor with data memberscardswhich is initially an empty array. It should also have the following member functions:- shuffle: shuffles the cards. Extra credit: what is the minimum number of swaps required to ensure that a deck is shuffled?
- addCards(x): takes as input- xwhich can either be an array of Cards, or a Deck, or just a Card, and adds it to the deck.
- deal(): returns a card from the top of the deck and removes it from the deck.
 
The dream is incomplete until we share it with our fellow Americans.
 
 
 Posts
Posts
 
 
How do we distinguish between a Deck and a Card. typeof returns object for both.
ReplyDeleteRight now this is how I'm going about this:
function Card(suit, num) {
this.getClass = function () {return "card"; }; and the same for deck
then simply calling objectName.getClass()
wrong?
You can check which constructor created an object.
ReplyDeleted.constructor == Card
or
d.constructor == Deck