www.webdeveloper.com
Results 1 to 5 of 5

Thread: how to loop the math.min and math.max

  1. #1
    Join Date
    Dec 2009
    Posts
    12

    how to loop the math.min and math.max

    the program that i want to create will first let the user input how many execution he want to perform and will determine the highest and lowest number that he inputted.

    for example.


    prompt: how many execution you want to perform?
    i type: 5


    prompt:input number:10
    prompt:input number:8
    prompt:input number:5
    prompt:input number:0
    prompt:input number:1

    alert: the highest number is 10
    alert: the smallest number is 0


    i can do that by simply using this code.

    <html>
    <head>
    <title>
    Array example!
    </title>
    </head>
    <body>
    <script>
    var inputs=new Array()
    var z
    var c
    var b
    for(z=0;z<=8;z++)
    {
    inputs[z]=prompt("Enter a number: ")
    }

    var a=inputs.slice(0,4)

    c=Math.min(inputs[0],inputs[1],inputs[2],inputs[3],inputs[4],inputs[5],inputs[6])
    b=Math.max(inputs[0],inputs[1],inputs[2],inputs[3],inputs[4],inputs[5],inputs[6])


    document.write(inputs[0])
    document.write("<br>"+inputs[1])
    document.write("<br>"+inputs[2])
    document.write("<br>"+inputs[3])
    document.write("<br>"+inputs[4])
    document.write("<br>"+inputs[5])
    document.write("<br>"+inputs[6])
    document.write("<br>"+inputs[7])
    document.write("<br>"+inputs[8])




    alert(b+" is the highest number and " +c+ " is the smallest number")

    </script>
    </body>
    </html>




    but it only allows for the up to inputs[6] and the code is very long. i think i can use looping in it. but i dont know to use looping in math.min and math.max.



    please help me.

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    // Get all the numbers at once in the prompt string,
    // and use string.split or match to get the numbers into an array.
    //
    Code:
    var min= Infinity, max= -Infinity, arr, str= '';
    while(str== ''){
        str= prompt('Enter the numbers to compare, separated by commas','');
        arr= str.match(/(\.\d+|\d+(\.\d+)?)/g) || '';
    }
    // now you can loop through each member of arr and test max and min:
    //
    Code:
    for(var i=0,L=arr.length;i<L;i++){
        max= Math.max(arr[i], max)
        min= Math.min(arr[i], min);
    }
    alert('Range is from '+min+' to '+max);
    // You can skip the loop and use Math.max and Math.min
    // directly on an array of numbers,
    // but you need to apply the methods to the arrays:
    //
    Code:
    max= Math.max.apply('', arr);
    min= Math.min.apply('', arr);
    alert('Range is from '+min+' to '+max);
    // Or the simplest way, you could sort the array by value
    // and return the first and last members:
    //
    Code:
    arr= arr.sort(function(a, b){return a-b});
    alert('Range is from '+arr.shift()+' to '+arr.pop());

  3. #3
    Join Date
    Dec 2009
    Posts
    12
    hi mrhoo youre code is good, but i cant understand it very well, can you please explain it a little bit more?

    im very interested in this code

    arr= arr.sort(function(a, b){return a-b});
    alert('Range is from '+arr.shift()+' to '+arr.pop());


    but i cant apply it on my codes.



    by the way do you know a method that will get the first and last in the .sort?

    i have my newly revise code here it is.

    <html>
    <head>
    <title>
    Array example!
    </title>
    </head>
    <body>
    <script>
    var inputs=new Array()
    var z
    var c
    var b
    for(z=0;z<=8;z++)
    {
    inputs[z]=prompt("Enter a number: ")
    }

    c=inputs.slice(0,4)

    document.write(inputs+"<br>");

    a=inputs.reverse()
    document.write(a+"<br>");

    b=c.sort()
    document.write(b+"<br>")

    d=Math.min(b)
    e=Math.max(b)

    alert(inputs[0]+" is the smallest number")
    alert(inputs[4]+" is the largest number")


    </script>
    </body>
    </html>


    by using sort it will arrange the numbers from highest to lowest, but i dont know any method that will get the first and last digits so that it will determine the highest and lowest from the slice.
    Last edited by cary1234; 02-20-2010 at 08:40 PM.

  4. #4
    Join Date
    Feb 2006
    Posts
    2,927
    var array=[1,3,5,2,4];
    array.sort(function(a,b){return a-b});

    // array is now[1,2,3,4,5]

    var min=array.shift()
    // shift removes and returns the first item (1)
    var max=array.pop()
    // pop removes and returns the last item (5)

  5. #5
    Join Date
    Dec 2009
    Posts
    12

    Thank you!

    it works great!


    thank you very much.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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