# 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 &lt; numbers.length; i++)
string = string + numbers[i]+"\
``````

";
document.getElementById(“e”).value = string;
}

``````function sum()
{
if (numbers.length == 0) return 0;
for (var i=0,total=0; i &lt; 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>

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

&lt;h4&gt;Total (Sum):&lt;/h4&gt;
&lt;input type="text" id="Sum" value="0"&gt;&lt;/input&gt;&lt;/br&gt;

&lt;h4&gt;Average:&lt;/h4&gt;
&lt;input type="text" id="Mean" value="0"&gt;&lt;/input&gt;&lt;/br&gt;

&lt;/form&gt;
``````

</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/showthread.php?746348-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 &lt; 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

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.

``````function sum()
{
if (numbers.length == 0) return 0;
for (var i=0,total=0; i < numbers.length; i++)
total += numbers[i];
[COLOR=#ff0000][B]document.getElementById("Sum").value = m;[/B][/COLOR]
}
``````

What is the purpose of the m variable and where have you assigned a value to m ?

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;