Javascript: why does this not works?

why is this not working

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script type="text/javascript">

    function getSeason() {
    alert("hello")
    month = Date.getMonth();

    season = '';
    switch(month) {
        case '12':
        case '1':
        case '2':
            season = 'winter';
        break;
        case '3':
        case '4':
        case '5':
            season = 'spring';
        break;
        case '6':
        case '7':
        case '8':
            season = 'summer';
        break;
        case '9':
        case '10':
        case '11':
            season = 'fall';
        break;
    }
    alert(season);
}

    </script>
</head>
  <body onload="getSeason()">

</body>
</html>
westdhAsked:
Who is Participating?
 
effesCommented:
This
month = Date.getMonth();

Open in new window

doesn't work because you have to call getMonth on an actual Object of type Date. So change that to something like
now = new Date();
month = now.getMonth();

Open in new window


Then in all the lines that start  with "case" you have to remove the quotes, because you switch over a number (month). So, for example,
case '1':

Open in new window

should read
case 1:

Open in new window

Do that for all the case lines and you should get the message box that tells you the season.

The only problem left now, is that getMonth starts numbering the months with 0. So January is 0 and so on, up to December, which is 11. That means you'll have to change the cases to take that into account.

Hope that helps.
0
 
westdhAuthor Commented:
Thanks
0
 
effesCommented:
You're welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.