Date Validation
Programming Basics · JavaScript
TASK
Problem
Given `day` and `month` in a non-leap year, print `VALID` if the date exists, otherwise `INVALID`.
EXAMPLE
Example
31 4
INVALID
LIMITS
Constraints
1 ≤ month ≤ 12; 1 ≤ day ≤ 31
LEARN
Theory for this problem
+
### Date Validation
`if / else if / else` chooses a branch from boolean conditions. Comparisons such as `<`, `>=`, `===` and logical operators `&&`, `||`, `!` combine the exact rules that decide which output is valid.
NEED HELP?
Hints
+
['Translate every case from the statement into a boolean condition before writing branches.', 'Check the empty/invalid fallback explicitly instead of relying on `undefined` or `NaN`. For “Date Validation” in “Programming Basics”, verify that the exact output format matches the statement.']
ANSWER
Solution
+
const fs = require('fs');
const [day, month] = fs.readFileSync(0, 'utf8').trim().split(/\s+/).map(Number);
let daysInMonth;
if (month === 2) {
daysInMonth = 28;
} else if ([4, 6, 9, 11].includes(month)) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
console.log(day <= daysInMonth ? 'VALID' : 'INVALID');