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
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:
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.