Counting Cards Help?

Tell us what’s happening:
I tried this and it worked, to my surprise. Because I don’t understand why the if statement is not getting executed every time the switch runs?

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++;
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count--;
      break;
  }
  if(count>0){
    return count + " Bet";
  }
  else{
    return count + " Hold";
  }
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/counting-cards

It’s actually working. The output that you see on the left is the output of the last call to the cc function. If you plugged each call in console.log, you can see the output of each call (not visible on the challenge page, but you can see it in your browser’s console).

I made a quick repl to demonstrate:

So it’s just not visible in the challenge page?

Yeah. It’s also mentioned in a comment in the code. Testing your code in another environment (like repl.it) helps if you get stuck in a challenge and need to insert a couple of console.logs in the code to debug.

1 Like