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

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

#1

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

#2

I think the using an if/elseif statement works better for a solution to this challenge, but based on the way you are solving it with if/else if, you could use switch just as easy.

Instead of using multiple OR operators (||), your first if statement could be simplified as:

if (card >=2 && card <=6) {

Then, you could skip your first else if statement all together, because you do not need to modify count if it is a 7, 8, or 9.

Lastly, your final else if could be simplified as:

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

Putting it all together your if statements would be:

if (card >=2 && card <=6) {
  count += 1;
}
else if {card == 10 || (card >= 'A' && card <= 'Q')) {
  count -= 1;
}

I think the above or the more compact version below works better than using switch:

if (card <=6) {
  count += 1;
}
else if {card >= '10')) {
  count -= 1;
}

#3

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


#4

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


#5

Letters have a lexical order similar to how numbers have an order. When you compare one letter to another like ‘J’ > ‘A’, JavaScript understands this order. The card >= ‘A’ && card <= ‘Q’ condition works, because the only card letters are ‘A’, ‘K’, ‘J’, and ‘Q’ and all of those are greater or equal to ‘A’ and less than or equal to ‘Q’ with respect of their lexical order.