Basic JavaScript - Counting Cards

I used the switch method to evaluate different values for the card variable. If the card is b/w 2 and 6, it should add 1 to the count and if it’s b/w 10 and “A”, it should subtract 1 from the count. After that I used the if/else method so if the count is greater than 0 it should return count + " Hold";

It seems right but I can’t get my code to pass at all. Does anyone spot anything wrong?

Your code so far

let count = 0;

function cc(card) {
  // Only change code below this line
switch (card) {
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    return count++;
    break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
    return count--;
    break;
}

if (count > 0) {
  return count + " Bet";
} else {
  return count + " Hold";
}
  // 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

You don’t need to ‘return’ the values within the switch statement (as this returns out of the function as a whole, not just the switch statement). That’s why you have ‘break’ to escape the code block within the switch.

1 Like

Thank you Doug.

So basically once my ‘return’ statement was executed, it completely ignored the rest of the function.

It would be good to start debugging your functions yourself. You can do this by adding a function call, and logging its return value.

For example, anywhere outside the function definition, you could add this line.

console.log(cc(2));
1 Like

Understood.

Thanks Colin. Appreciate your help.