Personal Assistant
Programming Basics · JavaScript
TASK
Problem
Given `hour`, `temp`, and rain flag. Choose greeting: GOOD MORNING through 11, GOOD AFTERNOON through 17, else GOOD EVENING. Append advice after ` | `: TAKE UMBRELLA if raining, else WEAR JACKET if temp<10, else HAVE A NICE DAY.
EXAMPLE
Example
8 5 0
GOOD MORNING | WEAR JACKET
LIMITS
Constraints
0≤hour≤23; -50≤temp≤50; rain∈{0,1}
LEARN
Theory for this problem
+
### Personal Assistant
`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 membership checks from insertion, removal, or updates; this keeps the state logic clear.']
ANSWER
Solution
+
const fs = require('fs');
const [hour, temperature, rain] = fs.readFileSync(0, 'utf8').trim().split(/\s+/).map(Number);
let greeting;
if (hour <= 11) {
greeting = 'GOOD MORNING';
} else if (hour <= 17) {
greeting = 'GOOD AFTERNOON';
} else {
greeting = 'GOOD EVENING';
}
let advice;
if (rain === 1) {
advice = 'TAKE UMBRELLA';
} else if (temperature < 10) {
advice = 'WEAR JACKET';
} else {
advice = 'HAVE A NICE DAY';
}
console.log(greeting, '|', advice);