2 Replies - 2673 Views - Last Post: 21 November 2008 - 06:36 PM

#1 newbee_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-November 08

javascript-calculate mean

Post icon  Posted 20 November 2008 - 08:57 PM

Hello,

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.

I created an external javascript file and embedded inside the strict XHTML file
here is what I got :-
<?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>


I decide to write MeanCalculation function and standardDevationCalculation function and NUmberReader function

My NumberReader function is
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]);
  }
}


Is this function correct?
I need help in writing mean function
function meanCalculation()
		 {
			   var i;
			   var sum;
			  var mean;
			 for(int i = 0; i<this.n.length;i++)
			 {


Can anyone help me how can I write this function???
thanks!!!

Is This A Good Question/Topic? 0
  • +

Replies To: javascript-calculate mean

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: javascript-calculate mean

Posted 20 November 2008 - 09:21 PM

Well typically the mean is just the average. Take the numbers, add them up and divide by the number of values you added together. Give that n is your number of values (in an array)...

function meanCalculation()
{
     var i;
     var sum;
     var mean;

     // Loop through the array and add up the numbers into sum
     for(int i = 0; i<this.n.length;i++)
     {
          sum += n[i];
     }

     // Make sure you had at least 1 number so you don't divide by zero
     // Otherwise just return a mean of 0
     if (this.n.length > 0) {
          mean = sum / this.n.length;
     }
     else { mean = 0; }
}



This is just a basic idea since I am not sure what else is in your program but hopefully you get the idea off of it.

"At DIC we be mean figuring code ninjas... well that and we are just mean altogether!" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 newbee_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-November 08

Re: javascript-calculate mean

Posted 21 November 2008 - 06:36 PM

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

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]);
  }
}



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;
 }


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));
 }


function stdDeviation()
 {
   var stdDeviation;
   stdDeviation =Math.sqrt(this.varianceCalculation());
 }


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>");
 }


Thanks,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1