Positive, Negative, or Zero
Programming Basics · JavaScript
TASK
Problem
Given integer `n`, print `POSITIVE`, `NEGATIVE`, or `ZERO`.
EXAMPLE
Example
0
ZERO
LIMITS
Constraints
All numeric input values fit in JavaScript `Number`.
LEARN
Theory for this problem
+
### Positive, Negative, or Zero
`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 n = Number(input);
if (n > 0) {
console.log("POSITIVE");
} else {
if (n < 0) {
console.log("NEGATIVE");
} else {
console.log("ZERO");
}
}