Live From Code Noob Central

Follow me to the dark side - of programming and other geekeries

The Blackjack deal

Tonight, I am proud to post that I have successfully finished my JavaScript Blackjack game challenge on codeacademy.com! Of course there’s always room for improvement but give me a break, I have a NEARLY fully functionning blackjack game, which now only requires pulling cards from a deck to avoid generating the same card twice.

One challenge a day though! My challenge for today was to make the actual game more “readable”. 

The original code does not include creating suit names, or clearly identifying face cards, which makes it difficult to read when your output resembles: “you have card 7 of suit 2 and card 1 of suit 4 and dealer has card 4 of suit 1 and card 13 of suit 1”.

Not exactly user friendly. 

Codeacademy proposes to include the information mentionned above using switch statements. I have decided to take another route and used arrays. I created two new methods in my Card constructor, one to get the suit name and one to get the card number or face.

When the card is randomly generated using the deal function here below, thevariables s and n are only numbers. Suit goes from 0 to 3 (four suits) and card number goes from 0 to 13 (fourteen cards). 

The two methods getNumberLab and getSuitLab are then called when printing the hand, transforming the numeric variables, which correspond to positions in the suitName and numName arrays into actual cards and suits. 

Easy right?

You can find my complete code here: http://pastebin.com/fLmp0THi - all credit to codeacademy.