LOL FCC Counting Cards

So I was attempting to the FCC JavaScript challenge counting cards here’s the setup

In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and low cards remaining in the deck. This is called Card Counting.

Having more high cards remaining in the deck favors the player. Each card is assigned a value according to the table below. When the count is positive, the player should bet high. When the count is zero or negative, the player should bet low.

Count Change Cards
+1 2, 3, 4, 5, 6
0 7, 8, 9
-1 10, ‘J’, ‘Q’, ‘K’, ‘A’

You will write a card counting function. It will receive a card parameter and increment or decrement the global countvariable according to the card’s value (see table). The function will then return a string with the current count and the string"Bet" if the count is positive, or "Hold" if the count is zero or negative. The current count and the player’s decision ("Bet" or"Hold") should be separated by a single space.

So I thought I can probably setup a few if else statements and knock this on out the park. Man oh man how wrong I was I spent a good hour on this on trying different things to no avail. Here is when I finally gave up lol.

var count = 0;

function cc(card) {
var status = “”;

if (card<=6)
{ count++;}
if (card <= 9)
{ count = count + 0;}
if (card == 10)
{ count = count + 0;}
if (card ===”K”||”J”||”Q”||”A”)
{ count -= 1;}
if (count >0 )
{ return count +” Bet”;}
if (count <= 0)
{ return count +” Hold”;}

Obviously, that didn’t work. I even tried different variations adding the else statements. I had this thought in my head that I should probably be using a switch statement. But I kept telling myself that couldn’t be right. Boy I wish I would have tried it because behold the answer is

var count = 0;

function cc(card) {

switch(card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count = count + 1;
break;

case 7:
case 8:
case 9:
count = count + 0;
break;

case 10:
case “J”:
case “Q”:
case “K”:
case “A”:
count = count – 1;
break;

}

if (count > 0) {
return count + ‘ Bet’;
} else {
return count + ‘ Hold’;
}

}

Lesson learned today. Simply put… Don’t knock it till you try it!

 

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s