Skip to content

Instantly share code, notes, and snippets.

@d30jeff
Created November 4, 2016 13:47
Show Gist options
  • Save d30jeff/4732af965ac8db348ed77e467c5cf2b1 to your computer and use it in GitHub Desktop.
Save d30jeff/4732af965ac8db348ed77e467c5cf2b1 to your computer and use it in GitHub Desktop.
Min Max
/**
* Get the smallest integer in an array.
* @param {Array} arr Array of numbers.
* @return {Integer} The smallest integer.
*/
function getMinimum(arr) {
var minValue = Infinity;
if (arr instanceof Array) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== null && arr[i] < minValue)
minValue = arr[i];
}
return minValue;
} else {
throw Error('Expecting array, bullshit given.');
}
}
/**
* Get the largest integer in an array.
* @param {Array} arr Array of numbers.
* @return {Integer} The largest integer.
*/
function getMaximum(arr) {
var maxValue = -Infinity;
if (arr instanceof Array) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== null && arr[i] > maxValue)
maxValue = arr[i]
}
return maxValue;
} else {
throw Error('Expecting array, bullshit given.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment