Im learning javascript, and I am struggling to get my case statement to work. The idea is that you enter any number between 1 - 7 in the form it will invoke a function and it will alert giving you the day of the week. The default is "not a valid day". It seems really simple but I thought I had done it right, I have a feeling its a variable problem......
function DayOfTheWeek()
var 1 = DayOfTheWeek;
var 2 = DayOfTheWeek;
var 3 = DayOfTheWeek;
var 4 = DayOfTheWeek;
var 5 = DayOfTheWeek;
var 6 = DayOfTheWeek;
var 7 = DayOfTheWeek;
switch (DayOfTheWeek){
case '1':
alert ("It's Monday");
break;
case 2:
alert ("It's Tuesday");
break;
case 3:
alert ("It's Wednsday");
break;
case 4:
alert ("It's Thursday");
break;
case 5:
alert ("It's Friday");
break;
case 6:
alert ("It's Saturday");
break;
case 7:
alert ("It's Sunday");
break;
default:
alert ("Not a valid day");
break;
}
Nearly all of your code is wrong.
Your forgot the { after the function is defined and the } at the end of the function. You want to use give a variable the same name like the function (it seems that you want to do this).
A variable name can't start with a number.
What you want could be:
function DayOfTheWeek(day) {
switch(day){
case 1:
alert ("It's Monday");
break;
case 2:
alert ("It's Tuesday");
break;
case 3:
alert ("It's Wednsday");
break;
case 4:
alert ("It's Thursday");
break;
case 5:
alert ("It's Friday");
break;
case 6:
alert ("It's Saturday");
break;
case 7:
alert ("It's Sunday");
break;
default:
alert ("Not a valid day");
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.