Skip to content

Instantly share code, notes, and snippets.

@basketofsoftkittens
Last active August 29, 2015 14:16
Show Gist options
  • Save basketofsoftkittens/6fc63a7078dfa6fcfeec to your computer and use it in GitHub Desktop.
Save basketofsoftkittens/6fc63a7078dfa6fcfeec to your computer and use it in GitHub Desktop.
Software Question
// Question 1) Please make this code work
add(4,3); // should equal 7
add(4)(3); // should equal 7
// Question 2) Please create a javascript class that represents a deck of cards. Please include any methods or properties on the class that you think might be applicable to using a deck of cards. Implementation details of methods are not neccessary
@CodePolymath
Copy link

var add = function(n1, n2){
    if (typeof n2 === 'number'){
        return n1 + n2;
    }
    return function(n2){
        return n1 + n2;
    };
};
console.log(add(3,5));
console.log(add(3)(5));

@CodePolymath
Copy link

function Card(index){
    this.index = index + 1;
    this.value = function(){
        var mod = Math.ceil(this.index / 4);
        switch(true){
            case mod < 10:
                return (mod + 1).toString();
            case mod === 10:
                return 'jack';
            case mod === 11:
                return 'queen';
            case mod === 12:
                return 'king';
            case mod === 13:
                return 'ace';
        }
    };
    this.suit = function(){
        var mod = this.index - Math.floor(this.index / 4)*4;
        switch(true){
        case mod % 4 === 0:
            return 'diamond';
        case mod % 3 === 0:
            return 'spade';
        case mod % 2 === 0:
            return 'heart';
        default:
            return 'club';
        }
    };
}
Card.prototype.cardname = function(){
    return this.value() + ' of ' + this.suit() + 's';
};

function Deck(){
    this.reset = function(){
        this.cards = [];
        for (var i = 0, l = 52; i < l; i++) {
            var card = new Card(i);
            this.cards.push(card);
        }
    };
    this.reset();
}

Deck.prototype.showAll = function(){
    for (var i = 0, l = this.cards.length; i < l; i++){
        console.log(this.cards[i].cardname());
    }
};

Deck.prototype.count = function(){
    return this.cards.length;
};

Deck.prototype.deal = function(count){
    var counter = count || 1;
    var cards = [];
    counter = counter > 52 ? 52 : counter;
    var card;
    if (this.cards.length === 0) {
        console.log('Deck is empty!');
        return;
    }
    if (this.cards.length === 1) {
        card = this.cards[0];
        this.cards = [];
        cards.push(card);
        console.log(card.cardname());
        return cards;
    }
    while (counter > 0) {
        var random = Math.floor(Math.random() * this.cards.length);
        card = this.cards.splice(random,1)[0];
        cards.push(card);
        console.log(card.cardname());
        counter -=1;
    }
    return cards;
};

var deck = new Deck();
deck.showAll();
var hand = deck.deal(5);
console.log(hand);
console.log(deck.count());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment