Created
September 10, 2012 12:17
-
-
Save gojun077/3690614 to your computer and use it in GitHub Desktop.
CodeAcademy - Getting Started with BJ - BJ Deal 'em Up Sec 2/2, Ex 5/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
/* | |
5. Improving getValue | |
Now that we have getValue in its own function, we can add some more code | |
there to make our scoring function more accurate. | |
Right now getValue returns the number of the card itself, so score | |
effectively returns card1+ card2 and we haven't really improved anything. | |
For example with card 30, which represents the 4 of clubs, we are giving | |
out 30 points instead of only 4. We are the worst casino ever. | |
So how can we get at the true value for a given card? In this case, how | |
can we translate card30 to 4 points? | |
What we really want is the remainder when the card is divided by 13, or | |
card modulo 13. This will give us the points value of the card we have. | |
(1) Define a function deal that declares a variable card. Assign to card | |
a random number between 1 and 52. Then return card. | |
(2) Declare variables card1 and card2. Assign to each variable a random value | |
by calling the deal function. | |
(3) Define a function called getValue. It has one parameter card. We want it | |
to return not card, but the remainder of card divided by 13. | |
(4) Write a function called score. Use the keyword return and return a score | |
which is the sum of card1 and card2 passed through the getValue function. | |
*/ | |
// Our deal function will return a random card | |
var deal = function() { | |
var card = Math.floor(Math.random()*52+1); | |
return card; | |
}; | |
// Deal out our first hand by declaring variables card1 and card2 | |
var card1 = deal(); | |
var card2 = deal(); | |
// Define a function getValue that returns the remainder when card | |
// is divided by 13 | |
var getValue = function(card) { | |
var resultvalue = card % 13; | |
return resultvalue; | |
}; | |
// Return the score of our hand | |
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