
December 7th, 2005
10:47 PM
Neverside Newbie
Status: Offline!
Javascript: finding lowest number in an array
Hey, another javascript question. I'm having problems finding the lowest value in an array and I can't work out why it doesnt want to work, help appreciated:
for (i = 0; i < split_result.length; i++) {
search1 = split_result[i].lastIndexOf(";");
weight[i] = split_result[i].slice((search1 + 1),split_result[i].length);
'psuedocode, orignals on another pc
if i == 0 then
set lowest as weight[0]
end if
if weight[i] < lowest then
lowest = weight[i]
end
}
array would be: weight[0]=8 weight[1]=3 etc
as it stands that methods not working too well, ill put up the proper code soon, but is there a better way of doing this say with a min function or soemthing?

December 8th, 2005
10:16 AM
Neversidian
Status: Offline!
just loop through the array:
<script type="text/javascript">
var high = 0;
var high_key = -1;
var array = new Array();
array[2] = 700;
array[5] = 455;
array[7] = 2;
for(i = 0; i < array.length; i++)
if(array[i] > high){ high = array[i]; high_key = i; }
alert('Highest Value / Key: '+high+' / '+high_key);
</script>
___________________
-Developer
-Forum Leader
-NeverNET

December 8th, 2005
12:22 PM
Neversidian
Status: Offline!
lowest = eval("Math.min(" + weight.join(',') + ");");
assuming weight is your array.
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

December 8th, 2005
10:08 PM
Neversidian
Status: Offline!
nice one, BT :OO
___________________
-Developer
-Forum Leader
-NeverNET

December 9th, 2005
03:51 AM
Neversidian
Status: Offline!

___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

December 9th, 2005
04:29 PM
Neverside Newbie
Status: Offline!
:/ cant get it to work i've tried it inside and out of the loop, i've also tried:
lowest = eval(Math.min( + weight.join(',') + ));

December 9th, 2005
07:09 PM
Neverside Newbie
Status: Offline!
ok so I forgot about trying to do it out of an array, and did it this way:
for (i = 0; i < split_result.length; i++) {
search1 = split_result[i].lastIndexOf(";");
weight = split_result[i].slice((search1 + 1),split_result[i].length);
highlight = Math.min(weight,highlight);
}
thanks for the help guys 