Skip to content

Instantly share code, notes, and snippets.

@gojun077
Created September 10, 2012 11:48
Show Gist options
  • Save gojun077/3690482 to your computer and use it in GitHub Desktop.
Save gojun077/3690482 to your computer and use it in GitHub Desktop.
CodeAcademy - Getting Started w/ BJ - BJ Deal 'em Up Sec 2/2, Ex 4/7
/*
4. More Score
To improve the score function we will need code to convert from the
numeric value of the card (1 through 52) to its actual value (ace through king).
We will need a new function, which we will call getValue.
Look at the score function on line 16. Notice we now return
getValue(card1) + getValue(card2) instead of just card1 + card2.
This getValue function will eventually be used to take a card
(which we represent by a number between 1 and 52) as a parameter and
return the value of the card for scoring in Blackjack (1 through 11).
Again, notice how we introduce more functions to keep our code organized.
This new getValue function will make it easier to improve our score function
in future exercises.
Write a new function, getValue, which takes a single parameter, card
and for now just returns card.
It's OK that this function doesn't actually do anything yet— we will
spice it up in the next exercise.
*/
// Our deal function will return a random card
function deal() {
card = Math.floor(Math.random()*52+1);
return card;
}
// Deal out our first hand
var card1 = deal();
var card2 = deal();
// Make a getValue function here, which should convert a card to
// the value that card is worth
var getValue = function(card) {
return card;
};
// Our score function converts our cards to a score
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