简体   繁体   中英

Javascript If then else statement

I am trying to call a variable in an alert with an If then else statement.

I am creating the global variable currentmonth on page load. I tested that a value is loading into currentmonth and it is working.

I am also creating the global variables (datejanuary through datedecember) on page load. I have also tested those values and they are working.

Basically what I'm wanting to do is get the current month and compare it to a value from an array and display a message in an alert. For example, the message for January would be "Mini Golf – the golf may be mini, the competition will be big. To the winner go the spoils!" for February the message would be "Hike – it's not about the destination, it's about the journey."

Below is the function I created for this and I can't get it to work. What am I missing?

    <script>
    function dateMonth()
    {
    var currentdate = "";
    if (currentmonth == "January")
    {
    currentdate = datejanuary;
    }
    else if (currentmonth == "February")
    {
    currentdate = datefebruary;
    }
    else if (currentmonth == "March")
    {
    currentdate = datemarch;
    }
    else if (currentmonth == "April")
    {
    currentdate = dateapril;
    }
    else if (currentmonth == "May")
    {
    currentdate = datemay;
     }
     else if (currentmonth == "June")
     {
     currentdate = datejune;
     }
     else if (currentmonth == "July")
     {
     currentdate = datejuly;
     }
     else if (currentmonth == "August")
     {
      currentdate = dateaugust;
      }
      else if (currentmonth == "September")
     {
     currentdate = dateseptember;
      }
      else if (currentmonth == "October")
      {
      currentdate = dateoctober;
      }
      else if (currentmonth == "November")
      {
      currentdate = datenovember;
      }
      else
      {
      currentdate = datedecember;
      }
      {
      vex.dialog.alert(currentdate);   
      }
      </script>

How about simply:

var messages = [
   "Mini Golf – the golf may be mini, the competition will be big. To the winner go the ..",
   "Hike – it’s not about the destination, it’s about the journey.",
   "..."
];

alert( messages[new Date().getMonth()] );

( getMonth() == 0 for Jan)

or so if you really need to do with if and else

<script>
    function dateMonth()
    {
        var currentdate = "";
        var currentmonth = new Date().getMonth()
        if (currentmonth == 1)
        {
            currentdate = datejanuary; //Your text
        }
        else if (currentmonth == 2)
        {
            currentdate = datefebruary;
        }
        else if (currentmonth == 3)
        {
            currentdate = datemarch;
        }
        else if (currentmonth == 4)
        {
            currentdate = dateapril;
        }
        else if (currentmonth == 5)
        {
            currentdate = datemay;
        }
        else if (currentmonth == 6)
        {
            currentdate = datejune;
        }
        else if (currentmonth == 7)
        {
            currentdate = datejuly;
        }
        else if (currentmonth == 8)
        {
            currentdate = dateaugust;
        }
        else if (currentmonth == 9)
        {
            currentdate = dateseptember;
        }
        else if (currentmonth == 10)
        {
            currentdate = dateoctober;
        }
        else if (currentmonth == 11
        {
            currentdate = datenovember;
        }
        else
        {
            currentdate = datedecember;
        }
        vex.dialog.alert(currentdate);   
    }
</script>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM