Skip to content

Instantly share code, notes, and snippets.

@wallacemaxters
Created April 12, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallacemaxters/bfded74e753b9ce2818a73ebe077c46e to your computer and use it in GitHub Desktop.
Save wallacemaxters/bfded74e753b9ce2818a73ebe077c46e to your computer and use it in GitHub Desktop.
Gets the number in the middle
function middle() {
var numbers = Array.prototype.slice.call(arguments);
if (numbers.length % 2 == 0) throw Error("The number of parameters should be odd");
var min, max;
while (numbers.length > 1) {
min = Math.min.apply(null, numbers);
max = Math.max.apply(null, numbers);
numbers.splice(numbers.indexOf(min), 1);
numbers.splice(numbers.indexOf(max), 1);
}
return numbers[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment