Click to See Complete Forum and Search --> : pick the highest value from an array


bartl
07-25-2003, 03:06 AM
hello all,


i have an array like this:

ArrTotal = new Array();
ArrDesc = new Array();
ArrTotal[1] = 10;
ArrDesc[1] = 'Item a';
ArrTotal[2] = 5;
ArrDesc[2] = 'Item b';
ArrTotal[3] = 12;
ArrDesc[3] = 'Item c';
ArrTotal[4] = 18;
ArrDesc[4] = 'Item d';

And i need to know the higest value, which is in this case '18' in the element 4 and I need to display the description of this element 4 (item d)

how can i do this, i tried already to sort this array but i can't get further then this


regards,
bart

xataku_nakusute
07-25-2003, 03:47 AM
have you tried using the Math.max element?

Gollum
07-25-2003, 03:59 AM
Except that I don't think Math.max() works on arrays

However, it's not too hard a problem to solve...


var n = -1;
var nMax = Math.MIN_VALUE;
for ( var i = 1; i <=4; i++ )
if ( ArrTotal[i] > nMax )
{
nMax = ArrTotal[i];
n = i;
}


Now for your array, nMax will become 18 and n will become 4

bartl
07-25-2003, 04:20 AM
Thanks Gollum,

it's working

xataku_nakusute thank you for your time


bart