Counting Cards Challenge-can i use return after case instead of creating an if statement at the end?

Instead of using an if statement at the end of switch, can I just return the count and string (Bet or Hold) after case? I tried and doesn’t work, but I’m not sure I understand why we can’t do that. Thanks!

  **Your code so far**

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++;
  //return count + " Bet";
  break;
  case 7:
  case 8:
  case 9:
  //return count + " Hold";
  break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
  count--;
  //return count + " Hold";
  break;
}
if (count > 0){
  return count + " Bet";
}
else {
  return count + " Hold";
}


// Only change code above this line
}

console.log(cc(2)); console.log(cc('J')); console.log(cc(9)); console.log(cc(2)); console.log(cc(7));
console.log(count);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Counting Cards

Link to the challenge:

The return statement is not based on the value of the card but instead on the value of the count. You can have a card with the value of 2,3,…, or 6 result in a value of count that indicates "Hold" or "Bet". Same with 7-9 and 10-A. The value of the card has nothing to do with "Hold" or "Bet", only the value of count.

1 Like

I mean, it does work, it just doesn’t do what it’s supposed to do. JeremyLT explained why.

You can use a ternary for each of the 3 cases, but that would result in some code duplication you would avoid by having the condition after the switch (unless I didn’t think it through).

return count > 0 ? count + ' Bet' : count + ' Hold';
2 Likes

Thank you! I appreciate the clarification!

1 Like

Thanks. Didn’t even think about the possibility of using ternary.