Card Counting Solution feedback

I am looking for some feedback on my solution for the Card Counting Lesson. This solution worked but I am curious if anyone with more experience may have done it differently. For example I have an empty else statement. I did this because if card equals 7,8, or 9 you don’t have to do anything to the value of count but the function still needs to know what to do with 7,8, or 9, right? Also, Is the way I wrote the if statement the best way to do this? Mentioning card for each or statement feels redundant. Is there a short hand for that? Thanks in advance for your feed back! :grinning:

var count = 0;

function cc(card) {
  // Only change code below this line
if(card == 2 || card == 3 || card == 4 || card == 5 || card == 6) {
  count++;
} else if( card == 10 || card == "J" || card == "Q" || card == "K" || card == "A") {
  count--;
} else {

}

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');

If the else statement is empty, then you can omit it and nothing will change.

1 Like

If you have a lot of conditions like these, it’s often better/more readable to use a switch instead:

1 Like

Thank you, My first version of this solution used a switch statement actually. Maybe I should have stuck with that.

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