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

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: string[] = [
    "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: number, month: number): boolean {
    // JavaScript/TypeScript Date months are 0-based
    const firstDay: Date = new Date(year, month - 1, 1);

    const daysInMonth: number = new Date(year, month, 0).getDate();

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

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

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

    for (let m: number = 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

...