Skip to main content
1 of 7

Can you give me an example by refactoring one of the if statements?

Your Card object can be changed as follows:

Following your general style,

function Suit(name, sym) {
    this.getName = function () {
        return name;
    }

    this.getSymbol = function () {
        return sym;
    }
}

var hearts = new Suit("Hearts", "\u2665");
var clubs = new Suit("Clubs", "\u2663");
var diamonds = new Suit("Diamonds", "\u2666");
var spades = new Suit("Spades", "\u2660"); 
var suits = [hearts, clubs,  diamonds, spades];

function Rank(name, value) {
    this.getName = function() {
        return name;
    }
    
    this.getValue = function() {
        return value
    }
}

var ranks = [
    new Rank("Ace", 11),             
    new Rank("2", 2),             
    new Rank("3", 3),             
    new Rank("4", 4),             
    new Rank("5", 5),             
    new Rank("6", 6),
    new Rank("7", 7),
    new Rank("8", 8),
    new Rank("9", 9),
    new Rank("10", 10),
    new Rank("Jack", 10),
    new Rank("King", 10),
    new Rank("Queen", 10)
];

// Cards exist in Hands or Decks
function Card(suit, rank) {
    this.getCardSuit = function () {
        return suit.getName();
    };

    this.getCardNumber = function () {
        return rank.getName();
    };

    this.getCardValue = function () {
        return rank.getValue();
    };

}

And your deck creation accordingly changed from:

// Populate deck with 52 cards in order
for (var i = 0; i < 52; i++) {
    deckOfCards[i] = new Card(tempCardSuit, tempCardNumber);

    if (tempCardNumber < 13) {
        tempCardNumber++;
    } else {
        tempCardNumber = 1;
        tempCardSuit++;
    }
}

to:

for (var suitIdx = 0; suitIdx < suits.length; suitIdx ++) {
    for (var rankIdx = 0; rankIdx < ranks.length; rankIdx ++) {
        deckOfCards.push(new Card(suits[suitIdx], ranks[rankIdx]));
    }
}