+ 1

What is wrong with my coding? Why alert option not coming.!!!!

var age = prompt("plz enter age"); switch(age){ case age < 18: alert("you are child"); break; case age > 18: alert("you are adult "); break; case age > 35: alert("you are getting older"); break; }

26th May 2020, 12:34 AM
G. Yaseen
G. Yaseen - avatar
5 odpowiedzi
+ 3
When you use prompt() the input is returned as a string. You need to convert the input to an int using parseInt() https://www.w3schools.com/jsref/jsref_parseint.asp
26th May 2020, 12:38 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Also, switch doesn't work with conditionals like that. You'd need to use if statements. https://www.w3schools.com/js/js_switch.asp
26th May 2020, 12:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Just place the prompt inside the parseInt call parseInt(prompt("plz enter age"));
26th May 2020, 12:50 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Ok Thanks bro
26th May 2020, 12:55 AM
G. Yaseen
G. Yaseen - avatar
0
ChaoticDawg can you edit my code, and convert the output to an int using parselent. Because i have no knowledge of int and parselent.
26th May 2020, 12:48 AM
G. Yaseen
G. Yaseen - avatar