2

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;
}
3
  • 2
    Variables cannot start with a number. How else would the JavaScript parser distinguish a number from a variable? Commented Mar 25, 2012 at 13:12
  • Where's the rest of the function definition for DayOfTheWeek(), ignoring the unused (and impossible) numeric variables... Commented Mar 25, 2012 at 13:14
  • Also, you probably want to pass a parameter whose day of week you want to output. Commented Mar 25, 2012 at 13:15

1 Answer 1

5

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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.