Why does my switch case statement only execute the default case when i ask a user input for the expression? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does my switch case statement only execute the default case when i ask a user input for the expression?

var rll=window.prompt("Write a number!"); switch(rll){ case 1: alert("Ichi"); break; case 2: alert("Ni"); break; default: alert("N/A"); }

2nd Jul 2020, 6:33 AM
Ashwin Shrestha
Ashwin Shrestha - avatar
3 Answers
+ 5
This is because the input is a string, and your case is an integer Try : case "1", Or convert the input to an integer using parseInt https://code.sololearn.com/WeO9weNgnt0m/?ref=app
2nd Jul 2020, 6:40 AM
ifl
ifl - avatar
+ 1
seems like switch care about the data type. try switch(parseInt(rll))
2nd Jul 2020, 6:36 AM
Taste
Taste - avatar
0
Thanks
2nd Jul 2020, 6:44 AM
Ashwin Shrestha
Ashwin Shrestha - avatar