Basic JavaScript - Counting Cards

What’s wrong with my code? I know it’s certainly not the best solution, however I see it and I don’t know why I can’t pass all the tests.

Specifically, these two requirements are what I do wrong:
Cards Sequence 2, J, 9, 2, 7 should return the string 1 Bet
Cards Sequence 2, 2, 10 should return the string 1 Bet

The rest I do well apparently, but why exactly? I check it over and over again and I can’t find an error in the counter, thanks!

This is my code

let count = 0;

function cc(card) {
  // Only change code below this line
  switch (card) {
  case 2:
  count += 1
  return count + " Bet"
  break;
  case 3:
  count += 1
  return count + " Bet"
  break;
  case 4:
  count += 1
  return count + " Bet"
  break;
  case 5: 
  count += 1
  return count + " Bet"
  break;
  case 6:
  count += 1
  return count + " Bet"
  break;
  
  case 7:
  return count + " Hold"
  break;
  case 8:
  return count + " Hold"
  break;
  case 9:
  return count + " Hold"
  break;

  case 10:
  count -= 1
  return count + " Hold"
  break;
  case "J":
  count -= 1
  return count + " Hold"
  break;
  case "Q":
  count -= 1
  return count + " Hold"
  break;
  case "K":
  count -= 1
  return count + " Hold"
  break;
  case "A":
  count -= 1
  return count + " Hold"
  break;

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

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

Hello and welcome to the forum!

There’s a global count variable. This means that each time the function is invoked, the variable is incremented/decremented and is NOT reset to 0 each time.

So, when you have a sequence of cards, the value of count will change several times.

Consider the sequence 2, 2, 10 (as in one of your failed tests).
After the first card, count is 1.
After the second card, count is 2.
Now, when the card 10 is played, the value of count will decrement to 1.

Your switch statement will decrement count but then return the message ‘1 Hold’.
Do you see the issue?

Before you return out of the function, you need to check the value of count.

Two tips:

  1. You need only one return statement in your function.
  2. You can bunch cases together to avoid repetition.

You can see examples of how to bunch cases here: JavaScript Switch Statement

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.

You misunderstood the instruction.

One question, what is the input/condition for you to return Hold or Bet ?

  • purely based on the input “card” ?
  • or something else?

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