Next Day
Programming Basics · JavaScript
TASK
Problem
Given valid `day month` in a non-leap year, print the next date as `day month`.
EXAMPLE
Example
28 2
1 3
LIMITS
Constraints
date is valid; non-leap year
LEARN
Theory for this problem
+
### Next Day
`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.', 'Before printing, output only the computed result and do not add explanatory text that is absent from the required format.']
ANSWER
Solution
+
const fs = require('fs');
let [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;
}
if (day < daysInMonth) {
day++;
} else {
day = 1;
month = month === 12 ? 1 : month + 1;
}
console.log(day, month);