Absolute Difference
Programming Basics · JavaScript
TASK
Problem
Given `a` and `b`, print the absolute difference.
EXAMPLE
Example
3 10
7
LIMITS
Constraints
All numeric input values fit in JavaScript `Number`.
LEARN
Theory for this problem
+
### Absolute Difference
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.', 'Remember that file contents may end with a newline: keep reading the data separate from normalizing it.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [a, b] = input.trim().split(/\s+/).filter(Boolean).map(Number);
let result = Math.abs(a - b);
console.log(result);