Decide whether a year has a 29th of February.
What to build
A program that turns the calendar's rule into one clear condition.
Requirements
- Read a year.
- Print
leapif it is a leap year, otherwise printcommon. - A year is a leap year when it divides by 4, except that years dividing by 100 are not, except that years dividing by 400 are.
Input
One whole number: the year.
Output
One word: leap or common.
Constraints
The year is from 1 to 9999.
Example
Input:
2024
Output:
leap
Why it works
An Earth year is about 365.2425 days long. Adding a day every 4 years is slightly too much, so the rule removes three leap days every 400 years. The exceptions become a condition joined with "and" and "or".
Examples
Example 1 · Input
2024
Expected output
leap
Example 2 · Input
2023
Expected output
common
Hints
Hint 1
A year is a leap year if it divides by 4, but not by 100, unless it also divides by 400.
Hint 2
y % 4 === 0 tests that y divides by 4.
Hint 3
Combine the tests with && and ||, using brackets to make the grouping clear.
Solution
Show one correct solution (try it yourself first)
const year = Number(prompt()); const isLeap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; console.log(isLeap ? "leap" : "common");