Just for fun, here's an approach that just tests years divisible by four:
function getLeapYears(start, end) {
var startYear = Math.ceil(start / 4) * 4;
var endYear = Math.floor(end / 4) * 4;
var leapYears = [];
if (isNaN(startYear) || isNaN(endYear)) return ['Invalid input'];
while (startYear <= endYear) {
if ( startYear % 100 || !(startYear % 400)) {
leapYears.push(startYear);
}
startYear += 4;
}
return leapYears.length? leapYears : ['None'];
}
And here's one way (some might say not "best practice") to put it in a page:
Start year: <input id="i0"><br>
End year: <input id="i1"><br>
<button onclick="
var start = document.getElementById('i0');
var end = document.getElementById('i1');
var el = document.getElementById('s0');
if (start && end && el) {
el.innerHTML = getLeapYears(start.value, end.value).join('<br>');
}
">List leapyears</button><br>
<span id="s0"></span>