Counting Cards - what am I doing wrong?

Working on the Counting Cards code. The following code fails the test. I don’t know what is wrong. I have spent hours looking at it and re-typing it.

let count = 0;

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

let change = 0;
let bet = "";
switch (card) {
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    change = 1;
    break;
  case 7:
  case 8:
  case 9:
    change = 0;
    break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
    change = -1;
    break;
}
count = (count + change);
if (count > 0){
  bet = " Bet";
}else if (count <= 0) {
  bet = " Hold";
}
  return (count, bet) ;
  // Only change code above this line
}

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

now, if I put the following just before the final return:

console.log(count, bet);

The output before running the tests is:

1 Bet
2 Bet
2 Bet
1 Bet
0 Hold

Which would seem correct. I don’t understand what I am doing wrong.

Please help.

This isn’t what you are supposed to return.

The instructions state:
" 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."
What am I supposed to return if not the count and “Bet” or “Hold”?

You are returning a tuple of two values, not a single string with the current count and the Hold/Bet string.

Doh! (smacks head in frustration)

OK, I combined the 2 values into a string and returned the string. and it worked. I added:

returnString = count + bet;
  return (returnString) ;

Even though the output looks identical in the console, I guess it’s different (2 separate values) at the code level.

Thank you!!!

Don’t include this.
case 7:
case 8:
case 9:
change = 0;

That’s where the issue is coming from. It counted 0, hence not relevant.

remove your “change”.
“count” is given already

There is no need for that case, but including it is harmless.

Thanks for the replies, everyone. I’m not sure I understand what you mean in some cases. So I’ll start with @camperextraordinaire :

if I use this code (which fails):

let count = 0;

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

let change = 0;
let bet = "";
let returnString = ""
switch (card) {
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    change = 1;
    break;
  case 7:
  case 8:
  case 9:
    change = 0;
    break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
    change = -1;
    break;
}
count = (count + change);
if (count > 0){
  bet = " Bet";
}else if (count <= 0) {
  bet = " Hold";
}
console.log(count,bet)
  return (count, bet) ;
  // Only change code above this line
}

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

my console shows:
1 Bet
2 Bet
2 Bet
1 Bet
0 Hold

If I use this code (which passes) and print to the console before the final return

let count = 0;

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

let change = 0;
let bet = "";
let returnString = ""
switch (card) {
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    change = 1;
    break;
  case 7:
  case 8:
  case 9:
    change = 0;
    break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
    change = -1;
    break;
}
count = (count + change);
if (count > 0){
  bet = " Bet";
}else if (count <= 0) {
  bet = " Hold";
}
returnString = count + bet;
console.log(returnString)
  return (returnString) ;
  // Only change code above this line

I get:

1 Bet
2 Bet
2 Bet
1 Bet
0 Hold

They look identical to me

@G-Coding , @JeremyLT , you’re both right, that code isn’t needed, but it also isn’t causing any issues.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.