www.webdeveloper.com
+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Posts
    13

    help return max number

    when you pass an argument full of numbers into a function, how do you make the function return the largest number in the arguments? please i need this explained without the function actually defining/declaring any of the arguments/parameters in its parenthesis

  2. #2
    Join Date
    Mar 2011
    Posts
    792
    Try the Math.max() function.
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Jun 2012
    Posts
    13
    ah yes i know the Math.max ... but for educational purposes beause im new to JS, i wanted to know how it was done this way. or is it not practical?

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,059
    Quote Originally Posted by Foolly View Post
    ah yes i know the Math.max ... but for educational purposes beause im new to JS, i wanted to know how it was done this way. or is it not practical?
    You use the arguments property, which you can research.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Jun 2012
    Posts
    13
    Quote Originally Posted by Logic Ali View Post
    You use the arguments property, which you can research.
    i have but i cant get it working. i can only get it working when i reference the arguments in my function call. but i want a way to do this without defining parameters, incase the arguments passed into the function exceeds the function parameters. heres what i mean

    onclick="values(3,6,8);" will be passed to my function which is ready for them: function values(a,b,c).
    but what if my function wasnt ready, i.e more arguments where passed, like values(3,6,8,24,4) and their length exceeded my function parameters? i want a way to make the function be able to handle this and still work out the max number.

    im happy with the Math.max method but i would just like to see how this is done

  6. #6
    Join Date
    Feb 2003
    Location
    Chicago Area, IL
    Posts
    5,736
    The arguments object in a function is an Array-like object. It has a length property telling you how many arguments the function currently has passed in. You can also access each argument in array syntax:
    Code:
    arguments[0]
    arguments[1]

  7. #7
    Join Date
    Jul 2007
    Posts
    386
    You have cycle the numbers and compare.

    Simple example:
    Code:
    <script type="text/javascript">
    function maxNumber()
    {
        var max = '';
        for(var i in arguments)
        {
            if(arguments[i] > max)
                max = arguments[i];
        }
        
        alert(max);
    }
    
    maxNumber(1, 8, 5, 9, 3, 2);
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

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
HTML5 Development Center



Recent Articles