# freeCodeCamp Challenge Guide: Counting Cards

freeCodeCamp Challenge Guide: Counting Cards
0

Remember to use `Read-Search-Ask` if you get stuck. Try to pair program and write your own code

### Problem Explanation:

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.

Value 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 count variable 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.

• Change the code below `// Only change code below this line` and up to `// Only change code above this line`
• Ensure that you are editing the inside of the `cc` function.
• Use what you’ve learned to check the value of each card parameter passed into the function.
• Keep a running count of that number.
• If the final count is 1 or greater, return # Hold.
• If the final count is 0 or less, return # Bet.

Example Output:

• -3 Hold
• 5 Bet

## Hint: 1

Use a `switch` (or `else if`) statement to count the value of each card.

try to solve the problem now

## Hint: 2

Add/subtract the value of each card to variable count. If the card is worth 0, don’t do anything.

try to solve the problem now

## Hint: 3

After you’ve counted the cards, use an `if` statement to check the value of count. Also, make sure your `return` has a space between the number and the string.

try to solve the problem now

## Basic Code Solution:

``````function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;
}
if (count > 0){
return count + " Bet";
} else {
return count + " Hold";
}
// Only change code above this line
}
``````

### Code Explanation:

• Check the value of each card via a `switch` statement.
• The variable count:
• Increases by 1 if the card is a 2, 3, 4, 5, or 6.
• Since 7, 8, and 9 aren’t worth anything, we ignore those cards in our `switch` statement.
• Decreases by 1 if the card is a 10, ‘J’, ‘Q’, ‘K’, or ‘A’.
• Check the value of count and return the appropriate response.

Example Run

• `cc(2);` runs.
• The `switch` statement hits `case 2`, jumps down and adds 1 to the variable `count`.
• The `switch` statement then hits the `break` and `cc(3);` runs.
• This cycle continues until the final call is made, `cc('A');`.
• After the `switch` statement, the `if` statement checks `count`, which is now 0.
• This then drops down to the `else` statement, which will return 0 Hold.

Note: As mentioned earlier, the `switch` statement could have also been an `else if` statement.

## NOTES FOR CONTRIBUTIONS:

• DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
• Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

42 Likes

Thank you for this perfectly explained solution. I ended up solving the problem after the 2nd hint. Solving problems is such a rewarding feeling. I appreciate your efforts and hope to someday return the favor to others.

20 Likes

Thank you for this. Can you tell me why this does not work - I tried the code above but replaced:

case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;

with:

``````case (card >= 2 && card <= 6):
count++;
break;
``````

why doesn’t that change work when I try to run the solution?

1 Like

I was able to complete the challenge using switch, but is there anything inherently wrong with using the following?

function cc(card) {
// Only change code below this line
if (card > 1 && card < 7) {
count++;
}

else if (card == 10 || card ==‘J’ || card ==‘Q’ || card ==‘K’ || card ==‘A’) {
count–;
}

else {
count;
}

if (count <= 0) {
return (count + " Hold");
}

else {
return (count + " Bet");
}

Thank you.

5 Likes

Good question! I tried to do the same.

1 Like

I think you should use “cc==10” not “cards==10”, but i dont know for sure

1 Like

This does not work because (card >= 2 && card <= 6) is not the right format for a case. Cases are used in switches i.e.

function cc(card) {
switch (card) {
case 2:
count==;
break;

you need to put it as an if function.

function cc(card) {
if (card >= 2 && card <= 6) {
return count++;
}

1 Like

Imagine some code:

``````switch(card) {
case 2:
case 3:
case 4:
...
``````

The interpreter will evaluate the expression after `switch`, in this case `card`. Maybe this has a value of 3, I’ll use that in my example.

Then it will evaluate the expressions after the case statements, and compare them to the value of the switch expression.
2 has a value of 2, which is not 3, so go on.
3 has a value of 3, which is 3, so from here execute code until hitting `break` or the end of the `switch` block.

Your `(card >= 2 && card <= 6)` returns a `true` or `false`, which does not equal any of the possible card value. You could hack it into working, using

``````switch(true) {
case (card >= 2 && card <= 6):
count++;
break;
...
``````

as this switch statement will evaluate case expressions until one equals `true`, but that’s a hacky way for an if-elseif structure like

``````if (card >= 2 && card <= 6) {
count++;
} else if (card === 10 || typeof card === "string") {
count--;
}
``````

(many other ways to write those conditions)

7 Likes

Thats a good solution. I used if-else statements. Here is my solution:

``````
var count = 0;

function cc(card) {
// Only change code below this line

if (card == 2 || card == 3 || card == 4 || card ==5 || card ==6) {
count += 1;
}
else if (card == 7 || card == 8 || card == 9) {
count += 0;
}
else if (card == 10 || card == "J" || card == "Q" || card == "K" || card == "A") {
count -= 1;
}
if (count <= 0) {
return String(count) + " Hold";
}
else {
return String(count) + " Bet";
}

// Only change code above this line
}

// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');

``````
12 Likes

I knew when I completed this challenge that there had got to be a simpler way of doing it… If I had left out the 7, 8 and 9 I think my earlier suggestion would’ve worked (a switch solution), but I didn’t think to remove them, and whatever I did with the count for those lines, it was messed up.

So, here is my solution. It feels a bit ridiculous to even post it, because it’s so much longer than needed. But what the heck, maybe someone will at least have a laugh checking out all the unnecessary work, lol.

[details=Code here]
var count = 0;
function cc(card) {

``````while (card == 2) {
count++;
break;
}
while (card == 3) {
count++;
break;
}
while (card == 4) {
count++;
break;
}
while (card == 5) {
count++;
break;
}
while (card == 6) {
count++;
break;
}
while (card == 7) {
count = count;
break;
}
while (card == 8) {
count = count;
break;
}
while (card == 9) {
count = count;
break;
}
while (card == 10) {
count--;
break;
}
while (card == "J") {
count--;
break;
}
while (card == "Q") {
count--;
break;
}
while (card == "K") {
count--;
break;
}
while (card == "A") {
count--;
break;
}

if (count >= 1) {
return count + " Bet";
} else
return count + " Hold";

}[/details]``````
5 Likes

function cc(card) {
// Only change code below this line
if(card > 1 && card < 7){
count++;
}else if (card == 7 || card == 8 || card == 9){
count = count +0;
}else if (card == 10 || card == ‘J’ || card == ‘Q’ || card == ‘K’|| card == ‘A’){
count–;

}

if (count > 0){
return count + " Bet";
} else {
return count + " Hold";
}
// Only change code above this line
}

My code that works. I hope that helps.

var count = 0;

function cc(card) {
switch(card){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
count+=1;
break;
case 7:
case 8:
case 9:
count+=0;
break;
case 10:
case “J”:
case “Q”:
case “K”:
case “A”:
count-=1;
break;

}

if (count <=0){
return count + " Hold";
}
else {
return count + " Bet";
}

return count;
// Only change code above this line
}

// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

4 Likes

Thanks for the explanation. I nearly had it, but I was returning an array so the output would be something like:

[-5,“Hold”]

I didn’t think to use the +.

Cheers!

also, a problem with count flow or something - tried to make a string = "result to overcome, still a problem though (only takes first set for real)

var count = 0;
function cc(card) {
str= “result”;
if (card= 2, 3, 4, 5, 6){
count=count+1 ;}

else if (card= 10, ‘J’, ‘Q’, ‘K’, ‘A’){
count=count-1; }

if (count>0)
{result= count + ’ Bet’;}
else {result= count + ’ Hold’;}

return result;
} // I MEAN, IT IS ONLY WORKING FOR THE FULL FIRST SET, WHY?//

Hi everyone,
Thank you guys for adding to the discussion of this forum.
Reading the conclusions drawn by all campers that tried to contribute to this post I see that the pattern is:

• campers that used if/else to modify the value of count and then used if/else again to return count+bet/hold didn’t pass the challenge (I among those).

• campers that used switch (as proposed in the official answer) or while (as proposed by @Lunaire86) in conjunction with if/else did succeed on the challenge.

Could these be true? Is it true only for this challenge? Is everyone using if/else wrong elsewhere?

Sorry this is my 3rd day programming and 2nd using JavaScript, so I genuinely have no idea, and google searches just return how to use if else on strings.
Regards

1 Like

Sorry, correcting myself, it seems that the problem is actually specifying every card, as @suhassrivats solved the whole thing only recurring to if/else.

This is what I like about programming. I was stuck because I was trying to calculate the cards that had value 0! How many times do we do this real life? acknowledging things that have 0 value? Thank you so much for the explanation!! Happy coding y’all!!!

1 Like

Hmm…kind of frustrating this one, maybe I just don’t have a knack for programming found a solution myself but it’s not nearly as clean…here’s mine:

var count = 0;

function cc(card) {
// Only change code below this line
var decision;
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) {
decision = “Bet”;
} else {
decision = “Hold”;
}

return (count + " " + decision);
// Only change code above this line
}

So basically I created an unnecessary extra variable and added the 7, 8, 9 cases also not needed…keep feeling I’m not cut out to be a programmer if I can’t figure out simple stuff like this…

2 Likes

I understood the coding part but I dont understand how does the input part work; since we have 2, 3, 4, 5, 6 here, so how would the interpreter input this data. I mean we have 5 cases here is that why we have five cc inputs. someone please help me as I am clearly not able to put my problem in words.

1 Like

Here is my more advanced solution which makes use of a regular expression.

``````var count = 0;
function cc(card) {
if (card <= 6) count++;
else if (/[1JQKA]/.test(card)) count--;
return count + (count > 0 ? ' Bet' : ' Hold');
}
``````
17 Likes