SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: sum & mean

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question sum & mean

    Hi,
    Im trying to make a calculator that on click displays the sum of the data in the array and provides the mean/ average.

    I have the formula for the mean = number of items(array.length?) * sum of array.

    Please help

    Can anyone recommend a superb/afordable/online javascript course?

    <html>
    <head>
    <title>Aggregates</title>

    <script type="text/javascript">



    var array = new Array();

    function insert(val)
    {
    array[array.length]=val;
    }

    function show()
    {
    var string="";
    for(i = 0; i < array.length; i++)

    document.getElementById("e").value = string =string+array[i]+"\n";

    }


    function sum(array)
    { numberTotal = 0
    for (var total = 0; total < array.length; total = total + 1)

    numberTotal = numberTotal + numberArray[total]

    return numberTotal

    document.getElementById("sum").value = numberTotal.value;
    }


    </script>


    </head>

    <body>
    <h1>Aggregates</h1>

    <h3>Add as many numbers as you like </br> to the list, then click Calculate.</h3>


    <form id="form">
    <input type="text" name="name" value="">
    <input type="button" value="Add to list" onClick="insert(this.form.name.value),show();"></br>
    <textarea name="txt" id="e" rows="10" ></textarea> </br>



    <h4>Total (Sum)/h4>
    <input type="text" id="Sum" value="0"></br>
    <input type="button" value="Calculate" onClick="sum(array);"></input>



    <h4>Average/h4>

    <input type="text" id="answer" value="0"></br>
    <input type="button" value="Reset" onClick="form.reset();" />
    </form>


    </body>

    </html>

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    function sum(numbers) { 
        if (numbers.length == 0) return 0;
        for (var i=0,total=0; i<array.length; i++) 
            total += numbers[i];
        return total; 
    }
    function mean(numbers) {
        return numbers.length == 0 ? 0 : sum(numbers)/numbers.length;
    }

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help!!

    appreciate it!

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Here's what is happening.

    When you click on Calculate, it runs this code:

    Code:
    sum(numbers);
    mean(numbers);
    So, where is the numbers variable coming from?
    Currently it's undefined.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The numbers variable is meant to be coming from the first array .

    value ="add to list" is supposed to add numerical data into an array

    which is to be displayed in a text area

    before being summed and averaged

    and displayed in a different area
    Last edited by minusten; Mar 29, 2011 at 19:38. Reason: more info

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by minusten View Post
    The numbers variable is meant to be coming from the first array .
    So what will you do. Will you rename numbers to be array, or rename array to be numbers.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you mean?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by minusten View Post
    I'm not sure what you mean?
    What I mean is, you know know the cause of the problem.
    So I'm asking, what do you want to do to solve it.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by paul_wilkins View Post
    So what will you do. Will you rename numbers to be array, or rename array to be numbers.
    what would be the two results of the two scenarios
    Last edited by minusten; Mar 29, 2011 at 20:03. Reason: clarity

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by minusten View Post
    what would be the two scenarios?
    The two scenarios are to rename numbers to be array, or rename array to be numbers.

    I prefer the latter as it then leaves the code with a more explanatory variable name.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that makes sense, like this?


    var numbers = new Array();

    function insert(val)
    {
    array[numbers.length]=val;
    }

    function show()
    {
    var string="";
    for(i = 0; i < array.length; i++)

    document.getElementById("e").value = string =string+array[i]+"\n";

  12. #12
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Quote Originally Posted by paul_wilkins View Post
    Here's what is happening.

    When you click on Calculate, it runs this code:

    Code:
    sum(numbers);
    mean(numbers);
    So, where is the numbers variable coming from?
    Currently it's undefined.
    I just got it well for the moment my fuctions are looking for (numbers) not (array)

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by minusten View Post
    that makes sense, like this?
    It applies to all references of that array variable.

    You'll find that it's not the only problem in the code. There are lots of problems that prevent it from working, but we can work through them steadily, understanding and solving the issues at hand.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much! I been going crazy with this, I really appreciate your help

    Ive rename the array to numbers for clarity and reference as suggested, This (val) part in blue-Is it correct/necessary?

    var numbers = new Array();

    function insert(val)
    {
    array[numbers.length]=val;
    }

    function show()
    {
    var string="";
    for(i = 0; i < array.length; i++)

    document.getElementById("e").value = string=string+array[i]+"\n";

    }

  15. #15
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by minusten View Post
    This (val) part in blue-Is it correct/necessary?
    Without it, it would be kind of hard for the function to know what number to add, so yes, it is necessary.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  16. #16
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For crying out loud...
    Code html:
    <html>
    <head>
    <title>Aggregates</title>
     
    <script type="text/javascript">
     
        var numbers = new Array();
     
        function insert(input) {
            var newNumber = input.value;
            input.value = "";
            if (isNan(newNumber)) {
                window.alert("Invalid number entered!");
                return;
            }
            numbers[numbers.length] = newNumber;
        }
     
        function show() {
            var string="";
            for(i = 0; i < numbers.length; i++)
                string = string + numbers[i]+"\n";
            document.getElementById("e").value = string;
        }
     
        function sum(id) { 
            if (numbers.length == 0) return 0;
            for (var i=0,total=0; i < numbers.length; i++) 
                total += numbers[i];
            if (id != null && id != "")
                document.getElementById(id).value = m;
            return total;
        }
     
        function mean(id) {
            var m = numbers.length == 0 ? 0 : sum(null)/numbers.length;
            document.getElementById(id).value = m;
            return m;
        }
     
        function resetNumbers() {
            numbers = new Array();
        }
    </script>
     
     
    </head>
     
    <body>
            <h1>Aggregates</h1>
            <h3>Add as many numbers as you like </br> to the list, then click Calculate.</h3>
            <form id="form">
                <input type="text" name="name" value="" />
                <input type="button" value="Add to list" onClick="insert(this.form.name);show();" /></br>
     
                <textarea name="txt" id="e" rows="10" ></textarea> </br>
     
                <h4>Total (Sum)/h4>
                <input type="text" id="Sum" value="0"></br>
                <input type="button" value="Calculate" onClick="sum('Sum');"></input> 
     
                <h4>Average/h4>
                <input type="text" id="Mean" value="0"></br>
                <input type="button" value="Mean" onClick="mean('Mean');"></input> 
     
                <input type="button" value="Reset" onClick="resetNumbers(); form.reset();" />
            </form>
        </body>
    </html>

  17. #17
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by transio View Post
    For crying out loud...
    Oh no, bad form old pal.

    Now large amounts of learning won't be achieved by that person, as we go through the details of what causes the problems and the aspects around them that lead to useful solutions.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  18. #18
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Oh no, bad form old pal.

    Now large amounts of learning won't be achieved by that person, as we go through the details of what causes the problems and the aspects around them that lead to useful solutions.
    It's obvious he/she isn't (interested in?) learning anything... it's been 24 hours, and he/she is still dilly-dalying with variable names... it's obvious he/she just wants someone to do his/her (home?) work for him/her.

    I'm not one to force someone to learn if they don't want to... if it is, indeed, homework, you're only cheating yourself I say... if it's something else... well maybe learning javascript isn't a priority in his/her life...

  19. #19
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PS - If I've robbed someone of the opportunity to learn, I apologize... if you want a free, good resource for learning Javascript interactively, and at your own pace, try W3Schools Online Web Tutorials - cheers.

  20. #20
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by transio View Post
    if you want a free, good resource for learning Javascript interactively, and at your own pace, try W3Schools Online Web Tutorials - cheers.
    Please do not use w3schools, as large parts of their material is out of date or incorrect. I don't say that lightly. Here is a long list of JavaScript w3schools corrections

    A more acurate place to learn about JavaScript is from places like Eloquent JavaScript
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  21. #21
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks paul is there a way i can pm you on this forum??

  22. #22
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by minusten View Post
    thanks paul is there a way i can pm you on this forum??
    Yes indeed. Click on my name for a list of options.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

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
  •