Issues with Counting Cards function

Issues with Counting Cards function
0

#1

Hello all.

I am asked to “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.”

In order to successfully complete the challenge, I must satisfy all seven of the below requirements:

Y = Complete
N = Incomplete

(Y) Cards Sequence 2, 3, 4, 5, 6 should return “5 Bet”

(Y) Cards Sequence 7, 8, 9 should return “0 Hold”

(N) Cards Sequence 10, J, Q, K, A should return “-5 Hold”

(Y) Cards Sequence 3, 7, Q, 8, A should return “-1 Hold”

(N) Cards Sequence 2, J, 9, 2, 7 should return “1 Bet”

(Y) Cards Sequence 2, 2, 10 should return “1 Bet”

(Y) Cards Sequence 3, 2, A, 10, K should return “-1 Hold”

Here is my current code:

var count = 0;

J = 11;
Q = 12;
K = 13;
A = 14;

minusFiveHold = [10, J, Q, K, A];
minusOneHold = [3, 7, Q, 8, A];
oneHold = [3, 2, A, 10, K];
oneBet = [2, J, 9, 2, 7];

function cc(card) {

 if (card >= 2 && card <= 6) {
   return "5 Bet";
 } else if (card >= 7 && card <= 9) {
   return "0 Hold";
 } else if (card >= 2 && card <= 10) {
   return "1 Bet";
 } else if (card >= 10 && card <= 14) {
   return "-5 Hold";
 } else if (minusOneHold || oneHold) {
   return "-1 Hold";
 } else if (card >= 2 && card <= 11) {
   return "1 Bet";
 }

}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(10); cc(7); cc(Q); cc(K); cc(A);

What’s weird is that when I hit enter to test my code, I returned an input of “-5 Hold”. However, as you can see above, I technically have not satisfied that requirement even though I have completed most of the others.

Plus, I was able to get one of the “1 Bet” sequences, but not the other. I initially had this line of code:

else if (card >= 2 && card <= 10) {
   return "1 Bet";
 }

written as:

else if (card >= 2 && card <= 10 || oneBet) {
   return "1 Bet";
 }

But when I did that, I saw that the “-1 Hold” card sequences had red Xs next to them. Not only that, I still wasn’t able to get both of the “1 Bet” sequences checked off.

I may be on the right track here, but I have a hunch that I’m not writing a couple lines of code correctly. Please let me know where I’m going wrong. Thanks to all who responded. I appreciate your input!


#2

Ok, so I found some problems with your code:

  1. You need to have a global counter, and increment/decrement the counter accordingly to which card you get.
    After that, you need to check the counter and decide on hold/bet
  2. You will be receiving a char for the letter cards as 'A', not a variable.

I recommend you focusing less in the tests and more in the description and you will better understand what it asks.


#3

if u still need an answer

var count = 0;

function cc(card) {

if(card >= 2 && card <=6)
{
count++;
}
else if(card === 10 || card == “J” || card == “Q” || card == “K” || card == “A”)
{
count–;
}
if(count > 0)
{
return count + " Bet";
}
return count + " Hold";

}
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);