Why switch not working?

valMetr = $(this).val();
//if it works
if(valMetr==0){
 valMetr = 0;
 }else if(valMetr>1 && valMetr<=49){
 valMetr = 30;
 }else if(valMetr>49 && valMetr<=69){
 valMetr = 35;
alert(valMetr);
 }else if(valMetr>69 && valMetr<=99){
 valMetr = 60;
 }else if(valMetr>99 && valMetr<=199){
 valMetr = 90;
}else{
 valMetr = 120;
 }


valMetr = $(this).val();
//switch works
switch(true){
 case valMetr == 0:
 valMetr = 0:
break;
 case valMetr>1 && valMetr<=49:
 valMetr = 30:
break;
 case valMetr>49 && valMetr<=69:
 valMetr = 35;
break;
 case valMetr>69 && valMetr<=99:
 valMetr = 60;
break;
 case valMetr>99 && valMetr<=199:
 valMetr = 90;
break;
default:
 valMetr = 120;
 }
July 2nd 19 at 17:56
3 answers
July 2nd 19 at 17:58
Solution
You have a colon where there should be a semicolon:

case valMetr == 0:
 valMetr = 0: // <<===== error should be a semicolon
break;
case valMetr>1 && valMetr<=49:
 valMetr = 30: // <<===== error should be a semicolon
 break;

Instead of such constructions of switch is better to use if ... else.
Faster will work, at least :-)
Thanks, I didn't even notice ) - Hattie.Purdy28 commented on July 2nd 19 at 18:01
July 2nd 19 at 18:00
Because you are preparing it wrong. In switch, you must have valMetr and case - value. For intervals switch-case is not very applicable.
July 2nd 19 at 18:02
something like this, but it is not the identify the best application switch
switch(valMetr){
 case 0:
 case 1:
 case 2:
// 47 more lines
 case 49:
 valMetr = 0:
 break;

Find more questions by tags JavaScript