+ 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");
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......
+ 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