Javascript: why does this not works?

Posted on 2011-10-04
Last Modified: 2012-05-12
why is this not working

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
    <title>Untitled Page</title>
    <script type="text/javascript">

    function getSeason() {
    month = Date.getMonth();

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

  <body onload="getSeason()">

Question by:westdh
    LVL 6

    Accepted Solution

    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.

    Author Closing Comment

    LVL 6

    Expert Comment

    You're welcome.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article will show, step by step, how to integrate R code into a R Sweave document
    Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
    In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now