Array Max


<html>
<head>
<title>Array Max</title>
<script type="text/javascript">
// From: http://codingforums.com/showthread.php?t=152260

var v=[10,8,42,50] // So looking to find 50
function srchMaxV() {
var maxV = 0; // or value smaller that smallest in array to search, like = -1000;
for (i=0; i if (v[i] > maxV) { maxV = v[i]; }
}
return maxV;
}

function NumSort(a,b) { return a-b; } // required for sorting numbers only
function sortMaxV() {
var sortedV = new Array();
sortedV = v.sort(NumSort);
alert('Max: '+sortedV[sortedV.length-1]+'\nMin: '+sortedV[0]);
}
</script>
</head>
<body>
<button onclick="alert('Max: '+srchMaxV())">Search for Max
<button onclick="sortMaxV()">Sort for Max/Min
</body>
</html>

Comments

Popular posts from this blog

PHP: Date Difference for PHP 5.2

HTML5 video loop src change on end play function

display the_content with setup_postdata