Skip to content
Advertisement

Writing a JavaScript program to calculate a leap year

I am writing a JavaScript program that takes in a date typed by the user and determines what day of the week the given date falls on. I am able to get the program to take in the date. I have used debug statements to print the values of the day, the month, the year, and the century to make sure the program is getting the values correctly, which they are so far.

I also have a debug variable called totalNumDays which stores the number of days for each month since that value differs. (January has 31 days, April has 30 days, February has either 28 or 29 days). The problem I am having is getting the number to be correct for the month February.

What I did was create an array of all the months with 31 days:

//1 = January, 3 = March, 5 = May,..., etc.
var monthNumsWith31 = [1, 3, 5, 7, 8, 10, 12];

Then later on I use a for loop to iterate through to determine the number of days. I tried a couple different things here. The one I thought made the most sense was:

for(var i = 0; i < monthNumsWith31.length; i++)
{
   if(month == monthNumsWith31[i])
   {
      totalNumDays = 31;
   }
   else
   {
      if(month == 2 && isLeapYear() == true)
      {
         totalNumDays = 29;
      }
      else if(month == 2 && isLeapYear() == false)
      {
         totalNumDays = 28;
      }
      else
      {
         totalNumDays = 30;
      }
   }

I tried a whole bunch of different things and can’t seem to get it to store the correct number of days for February. I even tried creating a second array of monthNumsWith30 and use two different for loops to iterate through both of those and set the appropriate number of days for months with 30 and 31 days. I think the problem might be in my isLeapYear() function. Here is the code for isLeapYear(). Note that year is global so the function does have access to it.

function isLeapYear() {
    if(year % 4 == 0)
    {
       if(year % 100 == 0)
       {
          if(year % 400 == 0)
          {
             return true;
          }
          else
          {
             return false;
          }
       }
       else
       {
          return true;
       }
    }
    else
    {
       return false;
    }
}

I tried to follow the formula for determining whether a year is a leap year or not. The formula can be seem here: https://gyazo.com/9e4b7fb92014d1e27315807c188fd5e0

Does anyone know why my function is not doing what it is supposed to be doing? Thanks.

UPDATE I got it working correctly now for figuring the correct number of days for each month. I am having another issue however.

To find out which day of the week a given date occurs on, I am using Zeller’s Congruence. The particular formula I am using is ((26M - 2) / 10 + D + Y + Y/4 + C/4 + 5C) MOD 7 where M, D, Y are exactly what they seem and C is the century (first two digits of the year). There is a part of the algorithm that states

IF Month < 3 THEN
   Year = Year - 1
   Month = Month + 10
ELSE
   Month = Month - 2
END IF

I got all that working seemingly correctly. I put in many debug statements to make sure it gets the correct values which it is, but for some reason the computer is evaluating the expression incorrectly.

This algorithm will give a number between 0 and 6. 0 being Sunday, 1 being Monday, etc. For example, taken the date 2/15/16. This date occurred on a Monday. Monday, February 15, 2016.

In my program I have the following code

var weekdayIndex = (Math.floor((26 * monthVal) - 2 / 10) + dayVal + 
    yearVal + Math.floor(yearVal / 4) + Math.floor(centuryVal / 4) +
    (5 * centuryVal)) % 7;

When this expression runs, it should equal 1, but for some reason it equals 0. I did the math by hand and evaluated each individual expression over and over again and kept getting 1. Doing this by hand I got the numbers to be (31 + 15 + 15 + 3 + 5 + 20) % 7. When I put this expression into the computer directly (without all the variable names and Math.floor expressions, just numbers) it correctly gets the value. I don’t know why it is incorrect when I use the variable names and expressions rather. Any ideas?

Advertisement

Answer

Your function is ok, except for a simple thing: you are missing the year parameter!

function isLeapYear(year) {
  return year % 4 == 0 &&
    (year % 100 !== 0 || year % 400 === 0);
}

But, with your extended syntax is ok too:

function isLeapYear(year) {
    if(year % 4 == 0)
    {
       if(year % 100 == 0)
       {
          if(year % 400 == 0)
          {
             return true;
          }
          else
          {
             return false;
          }
       }
       else
       {
          return true;
       }
    }
    else
    {
       return false;
    }
}

isLeapYear(1900) yields false, as expected, 2000 true, 1996 true, 1997 false.

Seems legit to me.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement