Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: I need a function to getMaximum number

  1. #1
    Join Date
    Apr 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need a function to getMaximum number

    function getMax()

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Get Maximum number in relation to what? A set of numbers fed in as parameters? You need to be more specific.

  3. #3
    Join Date
    Apr 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry, yes, lets say we have tree numbers.

  4. #4
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Well, there are many ways to do this. Here's a generic function that finds the largest number passed into its parameters, and returns it:

    Code:
    <script type="text/javascript">
    
    function largestnumber(){
    var largest=arguments[0]
    for (i=0; i<arguments.length; i++){
    if (arguments[i]>largest)
    largest=arguments[i]
    }
    return(largest)
    }
    
    </script>
    Usage example:

    Code:
    alert(largestnumber(3, 5, 9, 1, 3, 2)) alerts 9
    alert(largestnumber(23, 42, 12)) alerts 42

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ddadmin
    Well, there are many ways to do this.
    But the most sensible is to use the Math.max method, which can take any number of arguments, and is native (that is, quicker).

    Code:
    var largest = Math.max(a, b, c, ...);
    Mike

  6. #6
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Quote Originally Posted by mwinter
    But the most sensible is to use the Math.max method, which can take any number of arguments, and is native (that is, quicker).

    Code:
    var largest = Math.max(a, b, c, ...);
    Mike
    Hehe you're right, completely forgot about Math.max(). Actually, I did think of it, but for some reason remember it as only accepting two parameters.

  7. #7
    Join Date
    Apr 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What if I prompt the user to insert 3 numbers and then display the maximum number
    I tried to do it but it's not working

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by liffland
    What if I prompt the user to insert 3 numbers and then display the maximum number
    I tried to do it but it's not working
    Could you please link to, or post, what you've tried so far.

    Mike

  9. #9
    Join Date
    Apr 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    <html>
    <head>
    <title>Get Maximum Number</title>


    <script type="text/javascript">

    function getMax(num1, num2, num3){
    var largest=arguments[num1, num2, num3]
    for (i=0; i<arguments.length; i++){
    if (arguments[i]>largest)
    largest=arguments[i]
    }
    return(largest)
    }

    </script>


    </head>
    <body>
    <h2>Get Maximum Number</h2>


    <script language="JavaScript" type="text/javascript"><!--
    var num1=prompt("Enter a number: ", "")
    var num2=prompt("Enter another number: ", "")
    var num3=prompt("Enter the third number: ", "")
    var max= getMax(num1, num2, num3)
    document.write("You entered: ", num1, ", ", num2, ", ", num3, ", and the Maximum number is: ", max)

    // -->
    </script>

    </body>
    </html>

  10. #10
    Join Date
    Apr 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    sorry I give the wrong script
    here is the one I want you to see
    <html>
    <head>
    <title>Get Maximum Number</title>


    <script type="text/javascript">

    function getMax(num1, num2,num3){
    var largest=arguments[num1, num2, num3]
    for (i=0; i<arguments.length; i++){
    if ((num1>num2)&&(num1>num3))
    largest=num1
    else if((num2>num1)&&(num2>num3))
    largest=num2
    else
    largest=num3
    }
    return(largest)
    }

    </script>


    </head>
    <body>
    <h2>Get Maximum Number</h2>


    <script language="JavaScript" type="text/javascript"><!--
    var num1=prompt("Enter a number: ", "")
    var num2=prompt("Enter another number: ", "")
    var num3=prompt("Enter the third number: ", "")
    var max= getMax(num1, num2, num3)
    document.write("You entered: ", num1, ", ", num2, ", ", num3, ", and the Maximum number is: ", max)

    // -->
    </script>

    </body>
    </html>

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
  •