- Create a
Card(suit, number)
constructor with data memberssuit
and 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 equalsotherCard
.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
Deck
constructor with data members
cards
which 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 inputx
which 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.
[image: FTC rule finalized rule to protect from AI scams]
The Federal Trade Commission has approved a regulation prohibiting
impersonating a government age...
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