我试图在一个数字范围内找到闰年和非闰年。闰年和非闰年需要放入单独的数组中,稍后我会将它们输出到表格中。我对 JavaScript 很陌生,我怀疑我的循环可能是错误的,我不确定。
我的问题是非闰年数组没有得到它需要的所有数字。我输入 1900 - 2000 并且闰年似乎没问题,但非闰年数组只上升到 1930 年。我一直在尝试修复它一段时间,但我没有运气。
编辑:for
循环只供我测试数组。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>LeapYears</title>
<script type="text/javascript">
/* <![CDATA[ */
function calcLeapYear() {
var beginYear = document.leapYears.firstYear.value;
var endYear = document.leapYears.secondYear.value;
var leapYrs = new Array();
var nonLeapYrs = new Array();
var ctr = 0
var ctr2 = 0
while (beginYear < endYear){
if (((beginYear % 4 == 0) && (beginYear % 100 != 0)) || (beginYear % 400 == 0)) { /* Determines if year is a leap year or not */
leapYrs[ctr] = beginYear;
++ctr;
}
else {
nonLeapYrs[ctr2] = beginYear;
++ctr2;
}
++beginYear;
}
if (leapYrs == 0){
window.alert("There were no leap years within the range.");
}
for (i=0;i<leapYrs.length;i++){ //Testing Calculation
document.write(leapYrs[i] + "<br/>");
}
document.write("<br/>")
for (i=0;i<leapYrs.length;i++){
document.write(nonLeapYrs[i] + "<br/>");
}
}
/* ]]> */
</script>
</head>
<body>
<form name="leapYears">
Beginning Year: <input type="text" name="firstYear" /> End Year: <input type="text" name="secondYear" />
<input type="button" value="Find Leap Years" onclick="calcLeapYear()" />
</form>
</body>
</html>