JavaScript find the leap year of a given interval year

Claim:

The user enters the year interval to be judged, the start year and the end year, and outputs all leap years in this interval.
Insert picture description here
Insert picture description here
Insert picture description here

Code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function isRunYear(year) {
     
     
            // 是闰年返回true,否则返回false 
            var flag = false;
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
     
     
                flag = true;
            }
            return flag;
        }

        function printRunYear(start, end) {
     
     
            var runYear = [];
            // 定义数组储存闰年
            for (var i = start; i <= end; i++) {
     
     
                // 遍历设定的所有年份
                if (isRunYear(i)) {
     
     
                    runYear[runYear.length] = i;
                }
                // 调用isRunYear()函数,判断是否为闰年
                // 如果是闰年,则将该年份存到数组中
            }
            return runYear;
            // 返回闰年数组
        }
        var start = Number(prompt('请输入开始年份:'));
        var end = Number(prompt('请输入结束年份:'));
        var allRunYear = printRunYear(start, end);
        console.log(allRunYear);
    </script>
</body>

</html>

Guess you like

Origin blog.csdn.net/Jack_lzx/article/details/109241725