Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

146 users online



Javascript: finding lowest number in an array

Javascript: finding lowest number in an array

Currently viewing this thread: 1 (0 members and 1 guests)


neolumi

neolumi

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:

Code:

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?

aonic

aonic

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

BigToach

BigToach

Neversidian
Status: Offline!

lowest = eval("Math.min(" + weight.join(',') + ");");
assuming weight is your array.

___________________

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

aonic

aonic

Neversidian
Status: Offline!

nice one, BT :OO

___________________

-Developer
-Forum Leader
-NeverNET

BigToach

BigToach

Neversidian
Status: Offline!

Grin

___________________

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

neolumi

neolumi

Neverside Newbie
Status: Offline!

:/ cant get it to work i've tried it inside and out of the loop, i've also tried:

Code:

lowest = eval(Math.min( + weight.join(',') + ));

neolumi

neolumi

Neverside Newbie
Status: Offline!

ok so I forgot about trying to do it out of an array, and did it this way:

Code:

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 Smile

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0078 seconds.