Counting Cards "Which is better choice between if else or switch syntex to solve Cards Counting ?"

Tell us what’s happening:
I solved Counting Cards by if/else syntex, however Hint shows answer with switch.
I really wonder which is better as efficiency to choose between if and switch?
If we assume Counting cards as real project in professional world, which one btw if and switch programmers would choose?

Your code so far

var count = 0;

function cc(card) {
  // Only change code below this line
  if (card == 2 || card == 3 || card == 4 || card == 5 || card == 6 ) {
      count += 1;
  } else if (card == 7 || card == 8 || card == 9) {
      count += 0;
  } else if (card == 10 || card == 'J' || card == 'Q' || card == 'K' || card == 'A') {
      count -= 1;
  }
  
  if (count > 0) {
    return count + " Bet";    
  } 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(2); cc(10); ```
**Your browser information:**

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

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

I solved this problem with switch statement:

var count = 0;

function cc(card) {

switch(card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1;
break;

case 10:
case "J":
case "Q":
case "K":
case "A":
  count -= 1;   

}

if( count > 0 ) {
return count + " Bet";
}
return count + " Hold";
}
cc(2); cc(3); cc(7); cc(5); cc(4);

6 Likes

How does the logic in this section work?
I thought that you would need to define the “J” and “K” values too. Or am I missing something?
Thankss

else if {card == 10 || (card >= 'A' && card <= 'Q'))