Specifically w/r/t the broken logic in the first two lines of your switch
:
switch (card) {
case (>= 2 & <= 6):
...
Switch says: take the value in the brackets (switch (card)
), and if it matches a case, do the thing in that block.
So card
is either a number between 1 and 10 inclusive, or ‘A’, ‘J’, ‘Q’ or ‘K’.
So assume it is 5.
switch (5) {
case (>= 2 & <= 6):
The case isn’t valid javascript (or any programming language). >=
or <=
expect a comparison. So 1 <= 2
, x >= y
, etc, something on both sides. Leaving off one side makes there’s no comparison, it can’t ever work, and you just get a syntax error. So I assume you mean:
switch (card) {
case (card >= 2 & card <= 6):
...
So if card is 5, then 5 >= 2 & 5 <= 6
is 1.
5 is not 1, so that branch does not run. This is because
5 >= 2 & 5 <= 6
is
true & true
is
1 & 1
which is 1 BITWISE_AND 1. Which is 1.
I assume you meant
switch (card) {
case (card >= 2 && card <= 6):
...
So again assuming card
is 5, this evaluates to:
switch (5) {
case (5 >= 2 && 5 <= 6)
Which evaluates to
switch (5) {
case (true):
...
5 does not equal true
, so that branch does not run. 5 equals 5, not true
.
Edit: you can put logical operators in case statements but you need to remember that they evaluate to true
or false
. So if you want to do this (and this is not the point of this exercise, this is for future reference) you do not switch on card
, you switch on true
: if a case evaluates to true, run that branch:
switch (true) {
case card >= 2 && card <= 6:
// do stuff