PROBLEM 51
Medium

Middle Number

Programming Basics · JavaScript

</>
STATUS Not solved

TASK

Problem

#51

Given three distinct integers, print the middle value.

EXAMPLE

Example

Input
9 2 5
Output
5

LIMITS

Constraints

a, b, c are distinct
📖
LEARN Theory for this problem
+

### Middle Number

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.', 'Separate input parsing, result computation, and output into distinct steps so errors are easier to spot.']

ANSWER Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [a, b, c] = input.trim().split(/\s+/).filter(Boolean).map(Number);
if (((b < a) && (a < c)) || ((c < a) && (a < b))) {
    console.log(a);
} else {
    if (((a < b) && (b < c)) || ((c < b) && (b < a))) {
        console.log(b);
    } else {
        console.log(c);
    }
}