How to properly put <, >, <=, >= in case statements?


#1

Hi,

My code already works, but my question is how can I properly put <, >, <=, >=, etc in a case statement?

Here's my code:

var user = prompt("How many miles are you running?").toLowerCase();

switch(user) {
case '10':
console.log("Awesome!");
break;
case '13.1':
console.log("Half-marathon, eh?");
break;
case '26.2':
console.log("You're a marathon man!");
break;
** case '<= 9.9':**
** console.log("I need to run and keep up with you.");**
** break;**
default:
console.log("Off day?");

}

Now what I want to do is use any number less than 9.9 in order to get "I need to run and keep up with you." answer. Instead, when I type any number less than 9.9 it goes to the default answer, "Off day?". Is there a way to properly put these symbols (<, >, <= >=, etc) in the case statement, so that the "I need to run and keep up with you." answer is logged in the console? Do I need quotation marks to do it?

If I have confused you, please let me know. Otherwise, any help and advice will greatly appreciated. Thanks.


#2

Advice:
For me, i rather use if statement than switch case for that situation....


#3

Hi,

Thanks, I just realized it too. But now if I use if statement inside the case, I get an error message.

var user = prompt("How many miles are you running?").toLowerCase();

switch(user) {
case 10:
console.log("Awesome!");
break;
case 13.1:
console.log("Half-marathon, eh?");
break;
case 26.2:
console.log("You're a marathon man!");
break;
case 9.9:
if (miles <= 5) {
console.log("I need to run and keep up with you.");
}
else {
console.log("Too much for me, man!");
}
break;

default:
    console.log("Off day?");

}

Console.log automatically prints "Off day?" and gives this error message: "Oops, try again. Make sure to use at least one logical 'or' (||)! " Looks like something is off here..


#4

i think you modify it all, dont use switch case and convert it all to if else statement