Why switch statement is recommended over if else

Tell us what’s happening:
Cards Sequence 2, J, 9, 2, 7 should return

1 Bet

I think it should return 0 Hold, not sure why this condition is not satisfied

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;
case 7:
case 8:
case 9:
count = 0;
break;
}
var holdbet = 'Hold'
if (count > 0) {
  holdbet = 'Bet'
}

return count + " " + holdbet;

// Only change code above this line
}

cc(2); cc(); cc(7); cc('K'); cc('A');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

Read this …

Take a look at hint #1 in the challenge

the 2 raise by 1
so count is 1
the J lower by 1
so count is 0
9 does nothing
so count is 9
2 raise by 1
so count is 1
7 do nothing
so count is 1

when count is 1, function returns "Bet", so "1 Bet"


Do NOT reset count to 0 when value is 7, 8, or 9.

1 Like

To answer the question in your title:

Why switch statement is recommended over if else

It’s not. It’s recommended for certain applications. This application lends itself to switch. I think if you added proper formatting to the switch example and wrote out an example using if/else, you’d see why.

1 Like

Yes, I found the switch statements formatting being clear and short. Thanks Kevin.