编写JavaScript程序以计算a年 [英] Writing a JavaScript program to calculate a leap year

查看:36
本文介绍了编写JavaScript程序以计算a年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个JavaScript程序,该程序接受用户键入的日期并确定给定日期是星期几.我能够使该程序接受日期.我已经使用调试语句打印了日,月,年和世纪的值,以确保程序到目前为止正确地获取了这些值.

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.

我还有一个名为 totalNumDays 的调试变量,该变量存储每个月的天数,因为该值不同.(一月为31天,四月为30天,二月为28或29天).我遇到的问题是让2月份的数字正确无误.

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.

我所做的是创建了一个包含31天的所有月份的数组:

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];

然后,稍后我使用一个for循环迭代确定天数.我在这里尝试了几种不同的方法.我认为最有意义的是:

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;
      }
   }

我尝试了很多不同的方法,但似乎无法存储2月份的正确天数.我什至尝试创建第二个 monthNumsWith30 数组,并使用两个不同的for循环迭代这两个数组,并为30和31天的月份设置适当的天数.我认为问题可能出在我的 isLeapYear()函数中.这是 isLeapYear()的代码.请注意, year 是全局的,因此该函数可以访问它.

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;
    }
}

我试图遵循确定一年是否为a年的公式.该公式似乎在这里: https://gyazo.com/9e4b7fb92014d1e27315807c188fd5e0

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.

要查明给定日期是星期几,我使用的是Zeller的Congruence.我正在使用的特定公式是((26M-2)/10 + D + Y + Y/4 + C/4 + 5C)MOD 7 其中 M D Y 就是它们的外观, C 是世纪(一年的前两位数字).该算法的一部分说明了

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.

此算法将给出0到6之间的数字.0是星期日,1是星期一,依此类推.例如,日期为2/15/16.这个日期发生在星期一.2016年2月15日,星期一.

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;

此表达式运行时,应等于 1 ,但由于某种原因,它等于 0 .我手动进行数学运算,并一遍又一遍地评估每个表达式,并一直得到 1 .手动执行此操作,我得到的数字为(31 + 15 + 15 + 3 + 5 + 20)%7 .当我将此表达式直接放入计算机中时(没有所有变量名和Math.floor表达式,只是数字),它正确地获取了值.我不知道为什么当我使用变量名和表达式时它是不正确的.有什么想法吗?

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?

推荐答案

您的功能还可以,除了简单的事情:您缺少year参数!

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)产生 false ,正如预期的那样,2000 true ,1996 true ,1997 false .

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

对我来说似乎合法.

这篇关于编写JavaScript程序以计算a年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆