Skip to content Skip to sidebar Skip to footer

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 da

Solution 1:

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

functionisLeapYear(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)
          {
             returntrue;
          }
          else
          {
             returnfalse;
          }
       }
       else
       {
          returntrue;
       }
    }
    else
    {
       returnfalse;
    }
}

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

Seems legit to me.

Solution 2:

I think that your logic is rather suspect. I use the following function to determine whether a given year is a leap year:

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

Solution 3:

Here give this a shot. I assume you're just starting. We can avoid a lot of the nested if statements. Also, you'll want to start to get into the habit of using triple equals (===) as it does type checking and won't do weird things with "truthy" values.

First thing, looking at your isLeapYear function. When you do a check for an if statement and use the equal operator to compare them, it's going to return a true or false, so if we aren't doing weird things were we expect true to be false (though there are easy ways to do that too) we can actually just return the expression to get true or false, instead of needing to return true or false if your expression is true or false.

It also might be worth wild to pass in the year as an argument instead of getting it from the parent's scope. But in this case it doesn't actually matter, I'm sure.

The && will check if a statement is true, then it will execute the next expression. And you can chain it if you are expecting everything to be true, because if one thing is false, then everything is false. So no need to write nested if statements.

function checkLeapYear(yearToCheck) {
    return yearToCheck%4 === 0 && yearToCheck%100 === 0 && yearToCheck%400 === 0;
}

So looping through an array is kind of expensive. You might learn that if you start to loop through arrays within arrays. But luckily we don't even need that for this example.

This is a switch statement. It's very handy if you have values you need to equal the same thing. But you might hear from a lot of people the way I wrote mine is wrong and that you should never let a case fall through. I disagree with those people, because I think this is a great example of when you'd want to do just that.

This switch will take a variable, the month in this case. Compare it with the value in the cases, then execute the code for the case. It will fall through into the next case without a break; Because we want 1, 3, 5, 7, 8, 10, 12 to all make totalNumDays = 31 we set that for the lowest case and have the rest fall into.

But when if month equals 2, we want it totalNumDays to be 29 on leap years or 28 otherwise. Ternary Operator It basically works like a shorthand if statement. You give it an expression, in this case the checkLeapYear function, then a ? to start the ternary operator. Then the value for when it's true, this case we want it to equal 29. Then for false values use : then put want you want the false value to be, in this case 28.

In the switch statement, default just means do this if there are no matches. Which is making totalNumDays equal 30. Which this could be a bit dangerous as if we make month equal to 13 or 'A' it will still make totalNumDays equal 30. But preparing for every possibility isn't something I'm too concerned around right now.

switch(month) {
  case1:
  case3:
  case5:
  case7:
  case8:
  case10:
  case12:
    totalNumDays = 31;
    break;
  case2:
    totalNumDays = checkLeapYear(year) ? 29 : 28;
    break;
  default:
    totalNumDays = 30;
    break;
}

Anyway, I hope that helps and wasn't too much to take in. As for why your code isn't working. I think it might be because your loop starts on month 0. Which is going to be a 30 day month.

Solution 4:

The code actually works if the year parameter is used in the isLeapYear call and in the function declaration.

Here is the code exactly as posted (well, I added a missing closing } that I assume was a posting omission):

// Set some test valuesvar monthNumsWith31 = [1, 3, 5, 7, 8, 10, 12];
var month = 2;
var year = 2000for(var i = 0; i < monthNumsWith31.length; i++)
{
   if(month == monthNumsWith31[i])
   {
      totalNumDays = 31;
   }
   else
   {
      if(month == 2 && isLeapYear(year) == true)
      {
         totalNumDays = 29;
      }
      elseif(month == 2 && isLeapYear(year) == false)
      {
         totalNumDays = 28;
      }
      else
      {
         totalNumDays = 30;
      }
   }
}

functionisLeapYear(year) {
    if(year % 4 == 0)
    {
       if(year % 100 == 0)
       {
          if(year % 400 == 0)
          {
             returntrue;
          }
          else
          {
             returnfalse;
          }
       }
       else
       {
          returntrue;
       }
    }
    else
    {
       returnfalse;
    }
}

document.write(totalNumDays);

Post a Comment for "Writing A Javascript Program To Calculate A Leap Year"