Basic JavaScript - Counting Cards

Hello i have only one err:
" Cards Sequence 3, 7, Q, 8, A should return the string -1 Hold"

Doing the same things like code above and below, curly braces no matter.

Please help

let count = 0;

function cc(card) {
  // Only change code below this line
switch(card){
  case (2, 3, 4, 5, 6):
  count = "5 Bet";
  break;
  case (7, 8, 9):
  count = "0 Hold";
  break;
  case (10, 'J', 'Q', 'K', 'A'):
  count = "-5 Hold";
  break;
  case (3, 7, 'Q', 8, 'A'):
  count = "-1 Hold";
  break;
  case (2, 'J', 9, 2, 7):
  count = "1 Bet";
  break;
  case (2, 2, 10):
  count = "1 Bet";
  break;
  case (3, 2, 'A', 10, 'K'):
  count = "-1 Hold";
  break;

}

  return count;
  // 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

I see what you’re trying to do but you have misunderstood a little how the switch statement works and also how your code should handle the required logic.

You are supposed to increment/decrement the count variable each time a single card is played. So, set a switch case for each possible card value and act on the count variable accordingly.

Each time a card is played, the count will be adjusted, so when the main function is invoked several times (i.e. a sequence of cards), the final count is what is being tested, after the function has run several times.

Here’s an example with some possible card values. Note that if you wish to perform the same action for a number of different values, you can bunch cases together.

switch (card) {
case 2:
case 3:
  // do something here
  break;
case 7:
  // do something here
  break;
case 'J':
case 'Q':
  // do something here
  break;
}

Then, outside of your switch statement, you can check the value of count and return the required statement from your function.

2 Likes

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