www.webdeveloper.com
Results 1 to 4 of 4

Thread: calculation

  1. #1
    Join Date
    Jan 2013
    Posts
    1

    calculation

    Hi all,

    I am new in the web developing field and I'd like to ask you about a script I have to do.
    I need to create a form in JavaScript that will have a text field and a button. You'll import several numbers in the field, separated with comma. When you press the button will be appeared the following
    1)the minimum number
    2)the maximum number
    3)the average of the numbers

    I'd appreciate If someone could help me here..
    thank you so much!

    Mary

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Can you provide a sample of what you have attempted thus far?

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    <input id="data">
    <input type="button" value='compute' onclick="calc()">
    
    <script>
    function calc(){
      var r=data.value.split(/\s*,\s*/).map(Number);
      var mx=Math.max.apply(0, r);
      var mn=Math.min.apply(0, r);
      var avg=r.reduce(function(a,b){return a+b;}) / r.length;
     alert( [mn, mx, avg] );
    }
    </script>

  4. #4
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    593
    Code:
    <form>
    <input id="data">
    <input type="button" value='compute' onclick="calc()">
    </form>
    
    <script>
    function calc(){
      var Idata=document.forms[0].data.value;
      var Items=Idata.split(/,/);
      var Min=999, Max=-999, Total=0, Count=0;
      for (var i=0; i< Items.length; i++) {
        Item=Items[i];
        Count++;
        Total=Total*1+Item*1;
        if (Item*1 < Min*1) { Min=Item; }
        if (Item*1 > Max*1) { Max=Item; }
      }
      Average=Total/Count;
      alert('Minimum='+Min+' Maximum='+Max+' Total='+Total+' Count='+Count+' Average='+Average);
    }
    </script>
    Quote Originally Posted by rnd me View Post
    Code:
    <input id="data">
    <input type="button" value='compute' onclick="calc()">
    
    <script>
    function calc(){
      var r=data.value.split(/\s*,\s*/).map(Number);
      var mx=Math.max.apply(0, r);
      var mn=Math.min.apply(0, r);
      var avg=r.reduce(function(a,b){return a+b;}) / r.length;
     alert( [mn, mx, avg] );
    }
    </script>
    Please give me a hand at http://www.gofundme.com/bkzr98.

Thread Information

Users Browsing this Thread

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

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