# Counting Cards Code

Tell us what’s happening:
Everything works except "Cards Sequence 7, 8, 9 should return “0 Hold”.
Please I need help with and if there’s any problem with my code which is causing it not to work.
Thanks!

``````var 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 7:
case 8:
case 9:
break;

case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;
}

if (card > 0) {
return count + " Bet";
} else {
return count + " Hold";
}

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

// Note: Only the last will display
cc(2); cc("J"); cc(); cc(5); cc(6);
``````

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0`.

https://www.freecodecamp.org/challenges/counting-cards

Your approach to part of this challenge is wrong. You appear to be correctly incrementing/decrementing the count variable based on the cards’ values. However, what gets returned should be based on the count value and not the card value which you are currently doing.

okay thanks! But can you show me where within my code? is it with the switch statement?

What do you think the purpose of your switch section code is? What does it currently do?

My switch statement acts as something like a conditional to test the cards and to increase/decrease the count

Yes, that is true. So if the switch statement sets the value of count, then another section of your code should be using the count value to make a decision about what to return.

Oooh okay! That means the problem lies within the if/else statement

You seem to have it figured out now.

1 Like

Thanks for helping me to figure it out.

It now works

As a challenge to yourself, see if you can replace the Switch statement with if/else if statements to reduce the 22 lines the Switch statement takes up with an if/else if solution which only takes up 4 to 6 lines.

okay, i’ll try that one too

If you get stuck, just post your code and we will give you hints.

1 Like

hahaha… I think i’m stuck. Only one worked.

``````var count = 0;

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

var firstCards = [2, 3, 4, 5,6];
var secondCards = [7,8,9];
var thirdCards = [10, "J","Q","K","A"];

if (card == firstCards) {
count++;
return count + " Bet";
} else if (card == secondCards) {
count = 0;
} else {
count--;
return count + " Hold";
}

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

// Note: Only the last will display
cc(2); cc(3); cc(4); cc(5); cc(6);
``````

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

You really should not have modified the existing if else you had in the original code which used the count to determine the return value. I was only suggesting you replace the Switch statement portion. Your logic is way off when you try to combine the two sections, because they should be doing to different things.

If you are going to use the arrays, then you need to be able to check if a particular card value is in the arrays and depending on which array includes the value should determine to either increment or decrement count.
The issue you are facing is you can not compare a value (string or number) to an array of elements. Check out the indexOf function or includes function which will get you closer to a solution. Your approach is not the way I was thinking, but I believe it has potential to solve the problem with fewer lines of code.

//The problem is here as far as I noticed. You should return conditionally and use "count " variable. You do not need to anything with “card” value except take function parameters.

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