What would be the proper syntax to find the lowest of 5 user input numbers using IF statements?
| SitePoint Sponsor |
What would be the proper syntax to find the lowest of 5 user input numbers using IF statements?
I used this syntax but the number3 is always identified as largest even when it is not.
//largest
if (number1 > number2 > number3)
largest = number1;
else if (number2 > number1 > number3)
largest = number2;
else
largest=number3
You can't do it like that. You can only compare one item to another at a time. You can't compare 3 items to each other.
You can do the logical equivalent by using the and operator (&&) like this:
(the extra parentheses are optional here)
Code:if((number1 > number2) && (number1 > number3))
We miss you, Dan Schulz.
Learn CSS. | X/HTML Validator | CSS validator
Dynamic Site Solutions
Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.




largest= Math.max(number1 , number2 , number3)
or for an array of numbers-
var numberarray=[2,14,3,4,1,7]
var largest= Math.max.apply('',numberarray);
thanks for all the help!!!!!
Bookmarks