gpt4 book ai didi

javascript - Javascript 中的闰年(NEWBIE)

转载 作者:行者123 更新时间:2023-12-02 17:37:42 26 4
gpt4 key购买 nike

你好,我才刚刚开始使用基本的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com