 # Counting Cards

## Hints

### Hint 1

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

### Hint 2

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

### 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.

## Solutions

Solution 1 (Click to Show/Hide)
``````let count = 0;

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
}

cc(2); cc(3); cc(7); cc('K'); cc('A');
``````

#### 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.

Solution 2 (Click to Show/Hide)
``````function cc(card) {
// Only change code below this line
var regex = /[JQKA]/;
if (card > 1 && card < 7) {
count++;
} else if (card === 10 || regex.test(card)) {
count--;
}

if (count > 0) return count + " Bet";
return count + " Hold";

// Only change code above this line
}
``````

#### Code Explanation

· The function first evaluates `if` the condition `card` is a value greater than `1` and lower than `7`, in which case it increments `count` by one.
· Then if the card is `10` or higher it decrements `count` by one.
· The variable `regex` is a regular expression representing values (letters) for the higher cards.
· The `else` statement checks those values with the `|| (logical OR)` operator; first for `10` and then for any string that matches the regular expression using String.match().

99 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.

47 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?

4 Likes

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.

12 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++;
}

4 Likes

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)

18 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');

``````
20 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]``````
13 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’);

11 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

3 Likes

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!!!

4 Likes

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…

4 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.

2 Likes

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');
}
``````
34 Likes