+ 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

23rd Dec 2020, 8:04 AM
Áksháy Akshu
Áksháy Akshu - avatar
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/
23rd Dec 2020, 8:15 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
If you change what you have to an if-else if-else statement it'll work just fine.
23rd Dec 2020, 8:18 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
How i do for getting inputs from user
23rd Dec 2020, 8:16 AM
Áksháy Akshu
Áksháy Akshu - avatar
+ 1
Ok..i will try and thank you for solving my doubt
23rd Dec 2020, 8:20 AM
Áksháy Akshu
Áksháy Akshu - avatar
+ 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.
25th Dec 2020, 12:50 AM
AW4
AW4 - avatar
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?
23rd Dec 2020, 8:23 AM
Áksháy Akshu
Áksháy Akshu - avatar
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
23rd Dec 2020, 10:31 AM
JaScript
JaScript - avatar
0
Switch Statements doesn't work for Boolean. If you want to implement this conditional event, Use If else statements.
24th Dec 2020, 7:30 PM
Dheeraj Kumar
Dheeraj Kumar - avatar