Skip to content

Instantly share code, notes, and snippets.

@mrmlnc
Created March 29, 2017 20:24
Show Gist options
  • Save mrmlnc/6080c2ed183a8d1dbf8ee28c890d7d69 to your computer and use it in GitHub Desktop.
Save mrmlnc/6080c2ed183a8d1dbf8ee28c890d7d69 to your computer and use it in GitHub Desktop.
function median3(a: number, b: number, c: number): number {
if ((a - b) * (c - a) >= 0) {
return a;
} else if ((b - a) * (c - b) >= 0) {
return b;
}
return c;
}
console.log(2 === median3(1, 2, 3));
console.log(2 === median3(1, 3, 2));
console.log(2 === median3(3, 2, 1));
console.log(2 === median3(3, 1, 2));
console.log(2 === median3(2, 1, 3));
console.log(2 === median3(2, 3, 1));
console.log(2 === median3(1, 2, 2));
console.log(2 === median3(2, 1, 2));
console.log(2 === median3(2, 2, 1));
console.log(2 === median3(2, 2, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment