Created
April 12, 2017 18:46
-
-
Save wallacemaxters/bfded74e753b9ce2818a73ebe077c46e to your computer and use it in GitHub Desktop.
Gets the number in the middle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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