# 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.

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

<html>
<title>Aggregates</title>

``````&lt;script type="text/javascript"&gt;

var array = new Array();

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

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

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

";

``````		}

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

numberTotal = numberTotal + numberArray[total]

return numberTotal

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

&lt;/script&gt;
``````

<body>
<h1>Aggregates</h1>

``````&lt;h3&gt;Add as many numbers as you like &lt;/br&gt; to the list, then click Calculate.&lt;/h3&gt;

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

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

&lt;h4&gt;Average:&lt;/h4&gt;

&lt;input type="button" value="Reset" onClick="form.reset();" /&gt;
&lt;/form&gt;
``````

</body>

</html>:shifty::crazy:

``````
function sum(numbers) {
if (numbers.length == 0) return 0;
for (var i=0,total=0; i<array.length; i++)
total += numbers[i];
}
function mean(numbers) {
return numbers.length == 0 ? 0 : sum(numbers)/numbers.length;
}

``````

appreciate it!

Hereās what is happening.

When you click on Calculate, it runs this code:

``````
sum(numbers);
mean(numbers);

``````

So, where is the numbers variable coming from?
Currently itās undefined.

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

So what will you do. Will you rename numbers to be array, or rename array to be numbers.

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.

what would be the two results of the two scenarios:confused:

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.

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]+"
";

I just got it:D well for the moment my fuctions are looking for (numbers) not (array)

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.

:DThanks 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 &lt; array.length; i++)

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

";

``````		}
``````

For crying out loudā¦

``````
<html>
<title>Aggregates</title>

<script type="text/javascript">

var numbers = new Array();

function insert(input) {
var newNumber = input.value;
input.value = "";
if (isNan(newNumber)) {
return;
}
numbers[numbers.length] = newNumber;
}

function show() {
var string="";
for(i = 0; i < numbers.length; i++)
string = string + numbers[i]+"\
";
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;
}

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>

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

``````

Without it, it would be kind of hard for the function to know what number to add, so yes, it is necessary.

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ā¦

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.

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