javascript - Javascript 中的闰年(NEWBIE)

标签 javascript

你好,我才刚刚开始使用基本的 JavaScript。

Write a program which will accept a year from the user and display whether the year is a leap year or not.

老师没有提供太多帮助,我不知道应该如何进行编码,我不是在寻求答案 - 我想要一个链接或一些引用资料,我可以用来学习如何做到这一点因为我非常困惑。

下面是我认为它需要看起来像的一个例子,我现在才做 JS 几天,所以如果你要烧伤我,请记住这一点。

var year=prompt("Please input a year: ");

var remainder=year % 4
    if remainder === 4
        then 
document.write("It is not a leap year.");
        else
document.write("It is a leap year.");


document.write("You entered: " ,year, ".");

最佳答案

一些提示:

  • prompt 返回一个字符串,而不是数字。使用 parseInt(year, 10) 将其转换为数字。
  • if 语句看起来像 if (condition) { thendothis(); } else { dothis(); }
  • 根据定义,除以 4 的余数不可能是 4
  • 一年是闰年,如果:1) 能被 4 整除,2) 不能被 100 整除,除非 3) 能被 400 整除。2000 年是闰年,2100 年不会。您可能应该首先让它进行简单的除以 4 的操作,然后添加额外的规则。

关于javascript - Javascript 中的闰年(NEWBIE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22480061/

相关文章:

javascript - 多个可拖动的列 handle

javascript - 如何使用套接字将按钮的背景颜色数据发送到服务器

javascript - 当我选中/取消选中复选框时 Cookie 值发生变化

javascript - 如何将 thymeleaf 片段加载到 js 变量中

javascript - 未捕获类型错误 : Cannot read property of undefined - object

javascript - 包括 ActionController::Live 会中断现有的 AJAX 调用

javascript - ui-router 通过 URL 地址更改状态?

javascript - 如何在数组中添加多个值

javascript - jQuery Flickrush - 在图像周围环绕 <li>

javascript - 具有集成安全性的跨域 POST