Skip to content

Instantly share code, notes, and snippets.

@ybrdev
Last active December 31, 2015 00:19
Show Gist options
  • Save ybrdev/7906380 to your computer and use it in GitHub Desktop.
Save ybrdev/7906380 to your computer and use it in GitHub Desktop.
Function to find maximum and minimum value on JavaScript Array.
// Get maximum value from array
function findMax(array) {
var max = 0;
var a = array.length;
for (counter=0; counter<a; counter++) {
if (array[counter] > max) {
max = array[counter];
}
}
return max;
}
// Get minimum value from array
function findMin(array) {
var min = array[0];
var a = array.length;
for (counter=0; counter<a; counter++) {
if (array[counter] < min) {
min = array[counter];
}
}
return min;
}
// http://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array/20214662#20214662
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment