Created
September 11, 2012 13:25
-
-
Save gojun077/3698442 to your computer and use it in GitHub Desktop.
CodeAcademy - Getting Started with BJ - BJ Deal 'em Up Sec 2/2, Ex 6/7
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
/* 6. Don't Forget the Face Cards | |
Awesome! So we now have a somewhat realistic scoring function, | |
but we aren't quite there yet. We know that within a suit, cards | |
11, 12, and 0 represent a jack, queen, and king respectively. | |
Right now we count those as worth 11, 12, or 0 points, but in | |
real Blackjack all face cards are worth 10 points. Somehow in | |
our scoring function we need to check if we have a face card, | |
and make sure we only assign 10 points in that case. | |
What do we use to check a condition to decide what to do? | |
Sounds like a good job for an if statement! | |
Fill in the if statements in getValue to check whether card1 | |
and card2 are face cards. They are face cards if card % 13 | |
equals 0, 11 or 12. Return 10 points if they are face cards. | |
Otherwise, the number of points they get should be card % 13. | |
*/ | |
// Our deal function will return a random card | |
var deal = function () { | |
card = Math.floor(Math.random()*52+1); | |
return card; | |
}; | |
// Deal out our first hand | |
var card1 = deal(); | |
var card2 = deal(); | |
// This function takes a card as a parameter and returns the value | |
// of that card; If card modulo 13 is 0, 11, or 12, set card = 10 | |
var getValue = function(card) { | |
// if its a face card, number should be set to 10 | |
if (card % 13 > 10 || card % 13 === 0) { | |
return 10; | |
} | |
// Otherwise number should be set to card modulo 13 | |
else | |
return card % 13; | |
}; | |
// Here make a function called score, which will assign points based | |
// on the cards. It should take the remainder | |
var score = function() { | |
return getValue(card1) + getValue(card2); | |
}; | |
console.log("You have cards " + card1 + " and " + card2 + | |
" for a score of " + score()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment