+ 2
Why is not getting input
var age = prompt("Please enter your age."); switch (age) { case age <= 12: document.write("You are a child."); break; case age <= 18: document.write("You are a teenager."); break; case age > 18: document.write("You are getting old."); break; default: document.write("You did not enter an age."); } //Anyone can help me to solved this problem
8 Respostas
+ 4
The input is fine, but your switch cases are incorrect. Switch cases do not accept boolean expressions, but a matching case. This is why the default case is always running.
case 1:
...
case 2:
...
Please review the switch statement in the course.
https://www.sololearn.com/learn/JavaScript/1139/
+ 3
If you change what you have to an if-else if-else statement it'll work just fine.
+ 1
How i do for getting inputs from user
+ 1
Ok..i will try and thank you for solving my doubt
+ 1
case(1){if var: func};break;
u cant declare a case, only the instance of a case.
u cant declare a case as param.
0
Yeah, i know this syntax but i will try this using different method. But i think its not correct method to execute switch statement?
0
For this question switch is a bad choice because you need for it defined numbers such as 2, 7, etc. Better will be if statement. If you like this will help you:
https://code.sololearn.com/WaWRgLVpP5jF/?ref=app
0
Switch Statements doesn't work for Boolean.
If you want to implement this conditional event, Use If else statements.