• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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>
0
westdh
Asked:
westdh
  • 2
1 Solution
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now