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,930
    // 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 09:40 PM.

  4. #4
    Join Date
    Feb 2006
    Posts
    2,930
    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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.35583 seconds
  • Memory Usage 2,879KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates