Convert statement to switch case, comparison statements

Viewed 23

I am trying to convert this python code to switch: Python code I am trying to convert

However, I tried converting it and the only method I have found is using this:

var age = prompt("What is your age?");
age = parseInt(age);

switch(age) {  
case 1: case 2: case 3:
    console.log("You are under 4")
    break;
  default:
    console.log("I do not recognize your number")
}

I am aware that is not the full code from Python to switch case, but is there an easier way to do it than inputting every number individually by doing case:'number': rather than how you would normally sort of do it in python like this: (1<= number <= 3)

Any help would be much appreciated

1 Answers

My friend has helped me and has found an answer:

var number = parseInt(prompt('What is your number? '))
    switch (true)  {
    
    
    
         case (number >= 164 && number < 200):  
            console.log('You have been assigned the colour RED.')
            break;
        
        
        
         case (number >= 100 && number <= 163):
            console.log('You have been assigned the colour BLUE.')
            break;
        
        
        
         case (number >= 51 && number <= 99):
            console.log('You have been assigned the colour GREEN.')
            break;
        
        
        
         case (number >= 20 && number < 50):
            console.log('You have been assigned the colour YELLOW.')
            break;
        
        
        
         default:
            console.log('I do not recognise your number.')
        }
Related