Time of Day
Programming Basics · JavaScript
TASK
Problem
Given hour `h` 0..23, print `NIGHT`, `MORNING`, `DAY`, or `EVENING`.
EXAMPLE
Example
14
DAY
LIMITS
Constraints
0 ≤ h ≤ 23
LEARN
Theory for this problem
+
### Time of 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.', 'Match each input value type to the operation performed on it: JavaScript string and numeric behavior differ significantly.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let h = Number(input);
if (h <= 5) {
console.log("NIGHT");
} else {
if (h <= 11) {
console.log("MORNING");
} else {
if (h <= 17) {
console.log("DAY");
} else {
console.log("EVENING");
}
}
}