Why can't swtich statements be simplified like this?

Tell us what’s happening:
Describe your issue in detail here.

   **Your code so far**

var count = 0;

function cc(card) {
 // Only change code below this line
switch(card) {
  case (card > 2 || card < 6 ):
  count++ 
  break;
  case (card === 10 || card == "J" || card == "Q" || card == "K" || card == "A"):   
  count--
  break; 
} 

var holdBet = "Hold"

if (count > 0) {
  holdBet = "Bet"
}

 return count + " " + holdBet;


 // Only change code above this line
}

console.log(cc(7)); cc(3); cc(7); cc('K'); cc('A');
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Counting Cards

Link to the challenge:

The switch statement is basically doing this:

if(card === 'value') {
  // do something
}else if(card === 'other value') {
  // do something else
}

What happens if you try to do this?

if(card === (card > 2 || card < 6))

card is not a boolean, so this conditional will always be false.

I hope this helps :slight_smile:

1 Like

Yes thank you, It would work like that, but I am wondering why it is not possible to add a statement as a case when using the switch function.

you can, you can add anything but switch statement compare the argument with the case with ===, and you can’t change that

So that means it would only work with absolute true or false. Thank you!

Give the MDN article on switch a read as well.

1 Like

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