Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    help in calculating mean, variance, standard deviation

    I am supposed to write a program that prompts user to enter the numbers separated by space and when user click "ok" button, it should display the numbers entered with its mean, and standard deviation and the window should ask if user wants to enter numbers again if yes then it should display the new numbers entered, with its mean and standard deviation. the report method should use dynamic HTML so that each new report overlays the old one.

    I created an external javascript file and embedded inside the strict XHTML file

    I think I got all my functions...Can anyone look into my codes once again!! FOR SOME REASON IT IS NOT WORKING

    Code:
    function NumberReader()
    {
      var i;
      this.n = prompt("Enter a list of numbers separated by spaces:", "").split(" ");
      for(i=0;i<this.n.length;i++)
      {
        document.write("<br />["+i+"] = "+this.n[i]);
      }
    }
    Code:
    function meanCalculation()
    {
      var i;
      var sum;
      var mean;
      for(int i = 0; i<this.n.length;i++)
      {
         sum+=n[i];
       }
       mean=sum/this.n.length;
    }
    Code:
    function varianceCalculation()
    {
       var variance;
       var i;
       var sum;
       var meanValue;
       meanValue = this.meanCalculation();
       for(i = 0; i<this.n.length; i++)
       {
          sum[i]+= (this.n[i]-meanValue)*(this.n[i]-meanValue);
       }
       return (sum/(this.n.length -1));
    }
    Code:
    function stdDeviation()
    {
       var stdDeviation;
       stdDeviation =Math.sqrt(this.varianceCalculation());
    }
    Code:
    function report()
    {
       var reports;
       NumberReader();
       document.getElementById("Number list").innerHTML = reportContent;
       document.write("<p> mean:"+ this.meanCalculation()+".</p>");
       document.write("<p> variance:"+this.varianceCalculation()+".<p">);
       document.write("<p> standard deviation:"+ this.stdDeviation()+".</p>");
    }
    and here is html file:-
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> Calculator</title>
    <script type="text/javascript" src="mean.js"></script>
    </head>
    
    <body>
    <p>This page calculates mean and standard deviation</p>
    </body>
    <script type="text/javascript">
    var n=new Array();
    </script>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If I may say so, far too complicated with many assorted errors.

    Try this:-

    Code:
    <html>
    <head>
    <title> Calculator</title>
    </head>
    
    <body onload = "Another()">
    <p>This page calculates mean and standard deviation</p>
    
    <script type="text/javascript">
    
    var n=new Array();
    
    function CalculateMSD() {
     
    var n = prompt("Enter a list of numbers separated by spaces:", "").split(" ");
    for (var i=0;i<n.length;i++)  {
    if ((n[i] == "") || (isNaN(n[i]))) {
    alert ("Data Error");
    }
    document.write("<br />[" + i + "]  = " + n[i]);
    }
    
    var sum = 0;
    var mean = 0;
    for (var i = 0; i<n.length;i++) {
    sum += parseFloat(n[i]);   // must be a number
    }
    mean = sum/n.length;
    
    var variance = 0;
    for (var i = 0; i<n.length; i++)  {
    variance += (n[i]-mean)*(n[i]-mean);
    }
    variance = variance / (n.length-1)
    
    var stdDeviation = 0;
    stdDeviation = Math.sqrt(variance);
    
    document.write("<p> mean: " + mean + "</p>");
    document.write("<p> variance: " + variance + "<p>");
    document.write("<p> standard deviation: "+ stdDeviation + "</p>");
    
    Another();
    }
    
    function Another() {
    var cnf = confirm("Do you want to do another calculation?");
    if (cnf) {
    CalculateMSD();
    }
    }
     
    </script>
    
    </body>
    </html>
    At least once per year, some group of scientists will become very excited and announce that whatever they announced last year about global warming is wrong.
    Last edited by Philip M; 11-22-2008 at 10:57 AM. Reason: Improved

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Perhaps a little too complex and sophisticated for newbee_coder. His tutors will surely not believe that this script is his own work.

    One might suggest some sort of verification that the values entered by the user are in fact numbers, and that blank (null) entries are not made. As it is data 2, blank, 8, blank gives a mean of 2.50.

  • #4
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    thank you for the help. But I am not supposed to use another function, Another(). The only functions I can use are numberReader(), meanCalculation(), varianceCalculation(), stdDeviation() and report().
    I must use external javascript file, thats why I created external java script files.
    Can anyone help me rewriting my functions which I have already defined?
    report() should use dynamic HTML so that new report over lays the old report!

    Thanks again!!!

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by newbee_coder View Post
    I must use external javascript file, thats why I created external java script files.
    You do not need multiple .js files - all your functions (which are not really necessary) can go into one single file.

    If you want you can "reverse engineer" the script I gave you to separate out each section or code block into a separate function. Then you can place all these functions into one .js file. But make sure that values which are transferred from one function to another are global, that is declared outside the function.

  • #6
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    sorry for the misunderstanding. I just need one .js external file with 5 functions in it. Seems like my report() is not working. Like I said it should print user entered numbers, mean variance, sd and it should prompt user if users want to enter more numbers. If user enter more numbers then report() should print new numbers entered with its mean, variance, sd. New report should over lay old report.
    Also, how do I call one function in another method in javascript? I need meanCalculation function in varianceCalculation() function since we need mean to calculate variance.

    Thanks for the help!!!

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    As I say, you must make the variables mean, variance and stdDeviation global by declaring them outside any function.

    Then:

    document.write("<p> mean=: " + mean + ".</p>");
    document.write("<p> variance=: " + variance + ".(/<p">);
    document.write("<p> standard deviation = :" + stdDeviation + ".</p>")


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •