Clothes for Weather
Programming Basics · JavaScript
TASK
Problem
Given temperature `t` and rain flag 0/1. Base clothing: JACKET below10, HOODIE below20, else TSHIRT. Append ` UMBRELLA` if rain=1.
EXAMPLE
Example
15 1
HOODIE UMBRELLA
LIMITS
Constraints
rain ∈ {0,1}
LEARN
Theory for this problem
+
### Clothes for Weather
`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.', 'Mentally substitute at least one value for each branch and verify that exactly the intended branch runs.']
ANSWER
Solution
+
const fs = require('fs');
const [temperature, rain] = fs.readFileSync(0, 'utf8').trim().split(/\s+/).map(Number);
let clothes;
if (temperature < 10) {
clothes = 'JACKET';
} else if (temperature < 20) {
clothes = 'HOODIE';
} else {
clothes = 'TSHIRT';
}
if (rain === 1) {
clothes += ' UMBRELLA';
}
console.log(clothes);