Last active
August 29, 2015 14:16
-
-
Save basketofsoftkittens/6fc63a7078dfa6fcfeec to your computer and use it in GitHub Desktop.
Software Question
This file contains 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
// 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
commented
Mar 19, 2015
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