Previous Day
Programming Basics · JavaScript
TASK
Problem
Given valid `day month` in a non-leap year, print the previous date.
EXAMPLE
Example
1 3
28 2
LIMITS
Constraints
date is valid; non-leap year
LEARN
Theory for this problem
+
### Previous 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.', 'Separate input parsing, result computation, and output into distinct steps so errors are easier to spot.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [day, month] = input.trim().split(/\s+/).filter(Boolean).map(Number);
if (day > 1) {
day -= 1;
} else {
if ((month) === (1)) {
month = 12;
} else {
month -= 1;
}
if ((month) === (2)) {
day = 28;
} else {
if (((month) === (4)) || ((month) === (6)) || ((month) === (9)) || ((month) === (11))) {
day = 30;
} else {
day = 31;
}
}
}
console.log(day, month);