Suppose that I want to “switch case” in JavaScript. I want to deal with the possbility that a == 1, a > 1, or a < 1. So I bang out the following code:
function foobaz(a){
switch(a) {
case 1:
console.log("a is equal to 1");
break;
case a > 1:
console.log("a is greater than 1");
break;
case a < 1:
console.log("a is less than 1");
break;
default:
console.log("it's really hard to compare a to 1")
break;
}
}
foobaz(1);
foobaz(2);
foobaz(-4);
foobaz("The Queen of Hearts, she had some tarts!");
I’m expecting to get each of the four messages in sequence.
However, I actually get “a is equal to 1” and then “it’s really heard to compare a to 1” three times. Apparently I’m failing to properly define a > 1 and a < 1; how should I alter my code to achieve that effect?
Thanks.