0
Hello friends, "True", what is inside the parentheses of the "switch" command? Thank you for your help
var age=parseInt(window.prompt("Please enter your age")); switch(true){ case (age<12): window.alert("You are a child"); break case (age>=12 && age<=15): window.alert("You are a teenager"); break case (age>15 && age<=35): window.alert("You are young"); break case (age>35 && age<=60): window.alert("You are middle-aged"); break; case (age>60 && age<=100): window.alert("You are an old man"); break case (age>100): window.alert("You are not alive"); break default: window.alert("You entered the wrong age"); break; }
3 Respostas
+ 1
Would you like to know why they used true as switch expression?
+ 2
It's a method to use logical operations and conditions in switch statement.
Switch has to be true for prompt to work.
For example, age=50 is true for case (age<35 && age<=60).
So, output will be "You are middle-aged"
0
Yes bro