Welcome to my actionscript snippet library!

Actionscript Snips is a small repository of actionscript code that aims to save developers and designers time when developing a website.  

Find Min/Max Of Array Of Integers


var min:Number;
var max:Number;

var numbers:Array = new Array(3, 5, 2, 1, 4);

function findMin(array:Array):Number {
min = array[0];
for (var i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}


function findMax(array:Array):Number {
max = array[0];
for (var i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}


trace(findMin(numbers)); //Outputs 1
trace(findMax(numbers)); //Outputs 5


0 comments:

Post a Comment