Skip to content

Instantly share code, notes, and snippets.

@gojun077
Created September 10, 2012 10:58
Show Gist options
  • Save gojun077/3690317 to your computer and use it in GitHub Desktop.
Save gojun077/3690317 to your computer and use it in GitHub Desktop.
CodeAcademy - Getting Started with BJ - BJ Deal 'em up Sec 2/2, Ex 2/7
/*
2. Functionify Dealing
Nice! We are officially on our way!
Another important rule of thumb for bigger projects is to break
code down into functions, instead of throwing all your code
together into one place. This allows you to keep everything
organized and reuse code later on. This saves you lots of work!
So let's put dealing inside its own function calleddeal. That way,
we can use it later when we want to ask for more cards. So efficient!
We are playing blackjack so we'll need two cards.
(1) Write a function called deal. The body of the function should have
two lines of code. First, declare a variable card that generates a random
number between 1 and 52. Second, return card.
(2) Declare two variables called card1 andcard2 respectively. Assign to
each variable a random value that we get by calling the function.
(This method of dealing has one problem that we'll fix later. Can you
spot the way in which it is unrealistic?)
*/
// Define a function called deal
// It should return a random number between 1 and 52
var deal = function() {
var card = Math.floor(Math.random()*52+1);
return card;
};
// Declare two variables
// For both variables, assign values gotten by calling the function
var card1 = deal();
var card2 = deal();
//this is not realistic, because when we deal one card,
//we have 52 - 1 cards remaining and of course there are only 4 of
//each card in the entire deck; so we need to reduce the sample
//space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment