Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to determine which number is highest, surprisingly hard!

    Okay, I've got three variables: highPercent, mediumPercent, and lowPercent. The following code checks them to find the highest:

    Code:
    var highest = 100;
    if (highPercent >= mediumPercent && highPercent >= lowPercent) { highest = highPercent; }
    if (mediumPercent >= lowPercent && mediumPercent >= highPercent) { highest = mediumPercent; }
    if (lowPercent >= mediumPercent && lowPercent >= highPercent) { highest = lowPercent; }
    But with the following values:

    High: 29
    Medium: 64
    Low: 7

    It decides that High is the largest of the three.

    Why?

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Code:
    function max() { (arguments.slice = Array.prototype.slice)
    	if (arguments.length == 2)
    		return (arguments[0] > arguments[1]) ? arguments[0] : arguments[1];
    	else
    		return max(arguments[0], max.apply(max, arguments.slice(1)));
    }
    max(lowPercent, mediumPercent, highPercent)

    should return the correct one.

    Edit: Leave it to me to forget about the built-in Math.max() function.... Math.max(lowPercent, mediumPercent, highPercent)
    Last edited by jkd; 08-13-2004 at 05:41 AM.

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    doesn't max only take two values tho?

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    I don't think so, try the following


    <script>
    alert(Math.max(34,66,12,1,77,87))
    </script>

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh yeah, so it does!!


  •  

    Posting Permissions

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