How to print every month in a specified year that contains five full weekends (Fri, Sat, Sun) in JavaScript

1 Answer

0 votes
// A month has five full weekends when it has 31 days, and the 1st day of the month is a Friday.

// ------------------------------------------------------------
// Month names as a single reusable constant
// ------------------------------------------------------------
const monthNames = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
];

// ------------------------------------------------------------
// Function: returns true if a month has 5 full weekends
// ------------------------------------------------------------
function hasFiveFullWeekends(year, month) {
    // JavaScript months are 0-based, so month is 1–12 but Date uses 0–11
    const firstDay = new Date(year, month - 1, 1);

    // Get number of days in the month
    const daysInMonth = new Date(year, month, 0).getDate();

    // JS: Sunday=0, Monday=1, ..., Friday=5, Saturday=6
    const weekday = firstDay.getDay();

    return (daysInMonth === 31 && weekday === 5); // Friday
}

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
function main() {
    const y_value = 2026;

    for (let m = 1; m <= 12; m++) {
        if (hasFiveFullWeekends(y_value, m)) {
            console.log(`${monthNames[m - 1]} ${y_value} has five full weekends.`);
        }
    }
}

main();



/*
run:

May 2026 has five full weekends.

*/

 



answered May 26 by avibootz

Related questions

...