Who Is Older?
Programming Basics · JavaScript
TASK
Problem
Given ages `alice` and `bob`, print `ALICE`, `BOB`, or `SAME`.
EXAMPLE
Example
16 18
BOB
LIMITS
Constraints
0 ≤ age ≤ 120
LEARN
Theory for this problem
+
### Who Is Older?
JavaScript arithmetic works with numeric values stored in variables. A clear solution reads the needed values, computes the formula with `+`, `-`, `*`, `/` or `**`, and prints only the final result.
NEED HELP?
Hints
+
['Write the formula using named intermediate values if it contains more than one operation.', 'Order the checks so a more specific case is not swallowed by a broader condition.']
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("ALICE");
} else {
if (b > a) {
console.log("BOB");
} else {
console.log("SAME");
}
}