+ 2
PLEASE guys someone modify it i have no idea why all of them false
// Switch var age = prompt ("Please enter your age."); var age=1000 switch (age) { case age >=12: document.write("You are a child."); break; case age <= 18: document.write("You are a teenager."); break; case age < 98: document.write("You are getting old."); break; default: document.write("You did not enter an age."); }
7 Answers
+ 3
switch-case cannot be used for relational operators other than == (that too without writing the operator).
An example,
switch(age){
case 12: document.write("12!");break;
case 13:
document.write("13!");break;
default:
document.write("Not 12/13!");
}
If you want to check for other relations, use if-else if-else statements.
+ 3
yassin tafouzert ,thanks for that question and Nikhil thanks for that answer đđ.
I was also curious about the why after having also issues in setting correctly a valid switch and missed that part with strict equality.
Also about your code, don't forget to consider that prompt method return a value of type string (even if you enter a number), so it will also bring you to the default case (originaly I thought it was your only issue there).
So you'll need to convert that value with Number() method to check on strict equality or depending on how you'd like to use that value later.
+ 1
thank you so much i really was waiting for explaining like this thank you again
+ 1
You'r welcome, but just learning as you. Not a professional at allđ.
+ 1
But basically getting certificate here is only a matter of reading courses untill the end.
It gives you overview and starting point but being skillfull is not about the numbers of certificate you got here, so yes i'm still a beginner with little experience đ€·đ»ââïž.
0
Hmm
0
Please, I am a beginner
I need help