JavaScript prints the leap year of a given interval year

Time:2021-2-7

requirement:

The user inputs the year interval, start year and end year to be judged, and outputs all leap years in the interval.

Code implementation:

function isRunYear(year) {
    //Return true for leap year, otherwise return false 
    var flag = false;
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        flag = true;
    }
    return flag;
}

function printRunYear(start, end) {
    var runYear = [];
    //Define array to store leap year
    for (var i = start; i <= end; i++) {
        //Traverse all set years
        if (isRunYear(i)) {
            runYear[runYear.length] = i;
        }
        //Call the isrunyear() function to determine whether it is a leap year
        //If it is a leap year, the year is saved in the array
    }
    return runYear;
    //Returns an array of leap years
}
Var start = number (prompt ('Please enter start year: ');
Var end = number (prompt ('Please enter end year: '));
var allRunYear = printRunYear(start, end);
console.log(allRunYear);

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]