SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: if statements

  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if statements

    What would be the proper syntax to find the lowest of 5 user input numbers using IF statements?

  2. #2
    SitePoint Member
    Join Date
    May 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);

  5. #5
    SitePoint Member
    Join Date
    May 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    would this work for five numbers?

    thanks for all the help!!!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •