Skip to content

Instantly share code, notes, and snippets.

@gojun077
Created September 10, 2012 11:23
Show Gist options
  • Save gojun077/3690407 to your computer and use it in GitHub Desktop.
Save gojun077/3690407 to your computer and use it in GitHub Desktop.
CodeAcademy - Getting Started with BJ - BJ Deal 'em up Sec 2/2, Ex 3/7
/*
3. What’s the Score?
Great! We can deal out cards but what are we going to do with them?
The game won't be any good unless we have a way to score the hands.
For blackjack, that is relatively simple because the score is usually
just the number of the two cards added together. For example, a 10 of
spades and a 6 of hearts is worth 16 points.
For our design it's a little more complicated because card 30 is actually
the 4 of clubs, and shouldn't be worth 30 points. But remember our rule of
thumb from Exercise 1: start simple and then build up.
So let's start by just making our score equal to the sum of the two cards.
(1) Define a function deal that declares a variable card. Assign to card a
random number between 1 and 52. Then returncard.
(2) Declare variables card1 and card2. Assign to each variable a random value
by calling the deal function.
(3) Write a function called score. Use the keyword return and return a score
which is the sum of card1 and card2.
(4) Press run and see what prints out in the console!
*/
// Our deal function will return one random card
var deal = function() {
var card = Math.floor(Math.random()*52+1);
return card;
};
// Declare our two variables card1 and card2
var card1 = deal();
var card2 = deal();
// Define a function called score, which will assign points by
// adding up the cards:
var score = function () {
var total = card1 + card2;
return total;
};
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