Counting Cards. Am I on the right track?

Hi,

I am trying to do this challenge: https://www.freecodecamp.com/challenges/counting-cards

Is at least the idea right? The feedback says that it’s totally wrong.

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

return “Change Me”;
// Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

Your logic is not bad, but your function always returns with “Change Me”. If you read the description carefully, you will see that it should […] 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.

1 Like

Thanks. Now I got it totally right. I changed the last lines to this:

if (count > 0){return count + " " + “Bet”;}

else if(count < 0){return count + " " + “Hold”;}

else {return count + " " + “Hold”;}

return (count + “Bet” || count + “Hold”);

edit: and I also removed this completely: return “Change Me”;

No worries. By the way, what do you want to achieve with that last line? In your if-else statement you already returned so your function will never reach and execute the last line.

And as you can see you return with the same value in your else if and in your else. Don’t you think you could make it shorter? :slight_smile:

1 Like

I guess the last line is useless, because it would return/show the count and either string “Bet” or string “Hold”. The shorter way would be probably:

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

So there would not be need for else statement.