javascript - 我希望我的 JavaScript 代码返回“这是闰年”,但它返回“不是闰年”
问题描述
我希望我的 JavaScript 代码返回“这是闰年”,但它返回“不是闰年”。
function isLeap(year) {
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
return 'it is a leap year';
} else {
return 'not a leap year';
}
} else {
return 'It is a leap year';
}
} else {
return 'not a leap year';
}
}
console.log('2000: ', isLeap(2000));
console.log('2001: ', isLeap(2001));
console.log('2002: ', isLeap(2002));
console.log('2003: ', isLeap(2003));
console.log('2004: ', isLeap(2004));
解决方案
如果你只是调用isLeap();
,那么参数year
是undefined
. 你必须通过你想在你的函数中测试的年份,例如:isLeap(2020);
推荐阅读
- javascript - 为什么在使用反应路由器 dom 时未加载组件?
- php - 自动将表数据从 Postgresql 更新或同步到 Mysql,是否可以不使用 excel 导入或复制?
- asp.net-core - 使用配置文件进行集成测试
- c# - 使用 WebApplicationFactory 时如何使用 JWT Bearer Scheme
- c# - 我无法用日期转换一天
- angular - 这两种 Observable 语法有什么区别
- python-3.x - 将 Python2 十六进制变量转换为 Python3
- angular - 如何使导航组件在 Angular 中保持在顶部
- matlab - Ubuntu 中的 Matlab:为什么不能使用“系统”将结果存储到文件中?
- entity-framework-core - 如何使用 FromSqlRaw 在 Entity Framework Core 中返回 int 列表?