Would this work for challenge card counter?

I just want to see if I understood the concepts enough to make it work, I know it is not accepted as a format

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 10:

    case "J":

    case "Q":

    case "K":

    case "A":

      count--;

          break;  

  }

  switch (count){

    case count > 0:

      return count + "Bet";

        break;

    default :

      count + "Hold";

        break;

  }

  // Only change code above this line

}

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

You will write a card counting function. It will receive a card parameter, which can be a number or a string, 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.

Example Outputs: -3 Hold or 5 Bet

Hint
Do NOT reset count to 0 when value is 7, 8, or 9.
Do NOT return an array.
Do NOT include quotes (single or double) in the output.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

The switch for the card looks good.

This is going to cause a problem for you.

The cases must be exact values that match the value in count here

switch (count)

I’d just use an if-else here. You can bully a switch into acting like an if-else, but it’s not as clear as just using if-else.

Thank you! The last few sections have been switches so I was trying to keep using it in the same way to reinforce the pattern.

When I change it to

case count: 

It makes the bet lines work, but breaks the hold lines?

I’d try to get it to work with an if-else first. Then we can talk about how/if it could be made into a switch.

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

Now, you can make a switch do a same thing, but it’s ugly.

switch (true) {
  case count > 0:
    return "this is bad";
  case count <= 0:
    return "please don't";
}

It’s just a convoluted if-else though. Just use an if-else.

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