Smaller of Two
Programming Basics · JavaScript
TASK
Problem
Given `a` and `b`, print the smaller value.
EXAMPLE
Example
8 3
3
LIMITS
Constraints
All numeric input values fit in JavaScript `Number`.
LEARN
Theory for this problem
+
### Smaller of Two
`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 input = fs.readFileSync(0, 'utf8').trimEnd();
let [a, b] = input.trim().split(/\s+/).filter(Boolean).map(Number);
if (a <= b) {
console.log(a);
} else {
console.log(b);
}