Coordinate Quadrant
Programming Basics · JavaScript
TASK
Problem
Given non-zero point coordinates `x`, `y`, print quadrant number 1..4.
EXAMPLE
Example
-3 5
2
LIMITS
Constraints
x ≠ 0; y ≠ 0
LEARN
Theory for this problem
+
### Coordinate Quadrant
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.', '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 [x, y] = input.trim().split(/\s+/).filter(Boolean).map(Number);
if ((x > 0) && (y > 0)) {
console.log(1);
} else {
if ((x < 0) && (y > 0)) {
console.log(2);
} else {
if ((x < 0) && (y < 0)) {
console.log(3);
} else {
console.log(4);
}
}
}