PDA

View Full Version : Javascript Help!



designers_hub
5th April 2006, 04:55 PM
Is there a script which will pick up the highest number from a collection of numbers? the users will enter in numbers into seperate fields and i need to pick up the highest number.

any clues?

Kallikak
5th April 2006, 05:11 PM
Just loop through all the numbers and keep a record in a variable of the biggest number you've seen.

More generally, this site is a great resource for javascript snippets: http://www.irt.org/script/script.htm

Ken

geektechnu
5th April 2006, 05:57 PM
I'm thinking:

- Put the numbers into an array
- Set variable "highestNum" to 0
- For each element in the array as "x"
- Check whether x is greater than highestNum
- If true, highestNum = x, continue to next array element
- If false, highestNum stays the same, continue to next array element

When you reach the end, highestNum should contain the largest number.

Sorry it's not actual javascript here (haven't touched that stuff for years).
But the logic is all there. :P

iSlayer
5th April 2006, 06:26 PM
<script>
var numbers = Array ("1", "5", "3", "4", "2");
var highest = 0;
for ( x = 0; x < numbers.length; x++ ) {
if ( numbers[ x ] > highest )
highest = numbers[ x ]
}

document.write(highest)
</script>