I enter 2,Why am i getting default value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I enter 2,Why am i getting default value?

var day = prompt("enter the day"); switch (day<=7) { case 1: document.write("Monday"); break; case 2: document.write("Tuesday"); break; case 3: document.write("Wednesday"); break; default: document.write("Another day");

18th Mar 2018, 6:33 PM
Manoj Daiya
Manoj Daiya - avatar
2 Answers
+ 10
Replace the cases as shown below: case '1': case '2': case '3': Since you are taking input using prompt it stores the input as a string rather than number...... OR just write this statement var day = prompt ("enter day"); day=parseInt (day); . . . . . This makes the variable day to store the input data as an integer rather than string..... You Can use anyone one method which you feel easy......
18th Mar 2018, 6:53 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 5
Try it like this: var day = prompt("enter the day",1); switch (parseInt(day)) { case 1: document.write("Monday"); break; case 2: document.write("Tuesday"); break; case 3: document.write("Wednesday"); break; // more cases here... default: document.write("Another day"); } Hth, cmiiw
18th Mar 2018, 8:03 PM
Ipang