|
|
|||
Finding the Largest array
var foo = [1,20,254],i, largest= foo[0];
for(i=1; i<foo.length; i++){ if(foo[i]>largest){ largest = foo[i]; } } document.write( "<br /> " +"Largest no is: " + largest); Hi I Just started programming and write a very small code that is to find the largest value in an Array I want to know the working of this code,The code is working fine for me but actually I want to know how this code work.Mainly i'm confused in largest=foo[0] variable that i declare above. 2 Replies
The first line declares three variables:
If you think about this logically you need to compare the first element in the array to the next one. Whichever is largest is carried on and compared to the next element. You do this until there are no more elements and at the end you have the largest value.
=========================
Sent to you from my iPad, iPhone, BlackBerry, Laptop, Desktop, or Kitchen Toaster |
|||
|