# Thread: Sum and Average Calculator

1. ## Sum and Average Calculator

Hi,

I'm very new to Javascript and I've been trying to complete a 'simple' assignment for a few weeks now. Just wondering if anyone can help explain to me what is wrong with my code:

<html>

<title>Aggregates</title>

<script type="text/javascript">

var numbers = new Array();

function insert(input)
{
var newNumber = input.value;
input.value = "";
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()
{
if (numbers.length == 0) return 0;
for (var i=0,total=0; i < numbers.length; i++)
total += numbers[i];
document.getElementById("Sum").value = m;
}

function mean()
{
var m = numbers.length == 0 ? 0 : sum(null)/numbers.length;
document.getElementById("Mean").value = m;
return m;
}

function resetNumbers()
{
numbers = new Array();
}

</script>

<body>
<h1>Aggregates</h1>
<h3>Add as many numbers as you like </br> to the list, then click Calculate.</h3>
<form id="formtext" form name="formtext">
<input type="text" name="name" value=""></input>
<input type="button" value="Add to list" onClick="insert(document.formtext.name);show();"></input></br>

<textarea name="txt" id="e" rows="10" ></textarea> </br>
<input type="button" value="Calculate" onClick="sum();mean()"></input>
<input type="button" value="Reset" onClick="resetNumbers();form.reset()"></input>

<h4>Total (Sum)/h4>
<input type="text" id="Sum" value="0"></input></br>

<h4>Average/h4>
<input type="text" id="Mean" value="0"></input></br>

</form>
</body>

</html>

On opening it in IE is tells me 'm' is undefined, in function sum() and function mean(). I got most of the code from a previous thread http://www.sitepoint.com/forums/show...46348-sum-mean from someone else doing the same assignment and someone posted the answer....which doesn't work. I have been googling and found a few different ways of finding the sum and average of an array, but each way I've tried is not working. I tried merging the two functions together into function sumAverage:

function SumAverage()
{
var count
var Sum = 0
var Average
var result

// Find the lowest & highest
for (var i = 0; i < scores.length; i++)
Sum=Sum + scores[i]
Average = Sum/scores.length

but struggle how to correctly assignment the answer to the correct place using 'document.getElementById'.

Any help much appreciated, it's driving me crazy

Simon

2. On opening it in IE is tells me 'm' is undefined, in function sum() and function mean()
That error msg is telling you that you haven't assigned a value to m yet before trying to assign its value to another variable.

Code:
```function sum()
{
if (numbers.length == 0) return 0;
for (var i=0,total=0; i < numbers.length; i++)
total += numbers[i];
document.getElementById("Sum").value = m;
}```
What is the purpose of the m variable and where have you assigned a value to m ?

3. Hi,

Thanks for the reply, I've changed the m in function sum() to total:
function sum()
{
if (numbers.length == 0) return 0;
for (var i=0,total=0; i < numbers.length; i++)
total += parseInt(numbers[i]);
document.getElementById("Sum").value = total;
}
and it seems to work now, I also changed the numbers[i] to an integer. Just have to add comments, double check the code and add NaN and hopefully it's finished.

#### Posting Permissions

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