Created
September 11, 2012 13:35
-
-
Save gojun077/3698520 to your computer and use it in GitHub Desktop.
CodeAcademy - Getting Started with BJ - BJ Deal 'em Up Sec 2/2, Ex 7/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
/* 7. You're such an ace | |
We have one last special card to take care of, and that is | |
the ace. In real Blackjack, aces are worth either 11 or 1, | |
whichever helps your hand out more. | |
For now, let's again simplify by pretending aces are always | |
worth 11. | |
Modify getValue to check if we have an ace, and return 11 | |
points if we do. An ace is represented by a 1 in our game. | |
We now have the basics of dealing and scoring a hand! | |
As you do the next lessons, think about how what you learn | |
could improve our Blackjack game. We will return to it in a | |
few weeks to make it even better. | |
*/ | |
// 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 | |
var getValue = function(card) { | |
// if its a face card, number should be set to 10 | |
if (card % 13 > 10 | card % 13 === 0) { | |
return 10; | |
} | |
// What if it's an ace? | |
else if (card % 13 === 1) { | |
return 11; | |
} | |
// Otherwise, we just want its number value | |
else | |
return card % 13; | |
}; | |
// Score the hand | |
function score() { | |
return getValue(card1) + getValue(card2); | |
} | |
console.log("You have cards " + card1 + " and " + card2 + | |
" for a score of " + score(card1, card2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment