Blackjack game is driving me crazy

24 Jul

I’ve been working on the blackjack name for codecademy all day today without luck.  The challenge / puzzle / assignment is to have a hand of cards generated and scored.

The hand is an object that includes two ‘card’ objects. each card is generated randomly with two features (number and suit*).The suit is defined by a random number 1-4 and defined by a method for matching this numeral to a suit. The number is defined as 1-13 and set to values such that 1 (ace) = 11, 1-10 =face value and 11-13 are face cards worth 10pts each.


I simply can’t do it. My code looks reasonable to me (but I don’t really feel comfortable with it at all), but I’m getting crazy results. Here’s what I have, try copying it into a javascript field and see if you can make heads or tails of it:


// Make your card constructor again here, but make sure to use private
// variables!
function Card(s,n){
this.number = n;
this.suit = s;

this.getSuit = function(suit){
//console.log(“I’m about to switch”);
switch (this.suit){
case 1:
this.suit = “clubs”;
case 2:
this.suit = “diamonds”;
case 3:
this.suit = “hearts”;
case 4:
this.suit = “spades”;

this.getValue = function(number){
if(this.number === 1){
//console.log(“value = 11”);
this.value = 11;
return 11;
else if (this.number >=11){
//console.log(“value = 10”);
this.value = 10;
return 10;
//console.log(“value = “+ this.number);
this.value = number;
return this.number;

// Make a deal function here. It should return a new card with a suit
// that is a random number from 1 to 4, and a number that is a random
// number between 1 and 13
var deal = function(){
var suit = Math.floor(Math.random()*4)+1;
var number = Math.floor(Math.random()*13)+1;
return new Card(suit,number);

var Hand = function(){
this.card1 = deal();
console.log(“card1 is “+card1.number+ ” of “+card1.suit);
this.card2 = deal();
console.log(“card2 is “+card2.number+ ” of “+card2.suit);




*by the way, it is clear that there is nothing that prevents the same card from being drawn twice from what presumably is a single deck.

Leave a comment

Posted by on July 24, 2012 in Codecademy, Coding


Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: