# sum & mean

• Mar 28, 2011, 21:15
minusten
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>

<script type="text/javascript">

var array = new Array();

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

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

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

}

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

numberTotal = numberTotal + numberArray[total]

return numberTotal

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

</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.value),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(array);"></input>

<h4>Average:</h4>

<input type="text" id="answer" value="0"></br>
<input type="button" value="Reset" onClick="form.reset();" />
</form>

</body>

</html>:shifty::crazy:
• Mar 28, 2011, 21:22
transio
Code javascript:

```function sum(numbers) { if (numbers.length == 0) return 0; for (var i=0,total=0; i<array.length; i++) total += numbers[i]; return total; } function mean(numbers) { return numbers.length == 0 ? 0 : sum(numbers)/numbers.length; }```
• Mar 28, 2011, 22:14
minusten
Thanks for your help!!

appreciate it! :D
• Mar 29, 2011, 13:34
paul_wilkins
Here's what is happening.

When you click on Calculate, it runs this code:

Code:

```sum(numbers); mean(numbers);```
So, where is the numbers variable coming from?
Currently it's undefined.
• Mar 29, 2011, 18:35
minusten
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
• Mar 29, 2011, 18:37
paul_wilkins
Quote:

Originally Posted by minusten
The numbers variable is meant to be coming from the first array .

So what will you do. Will you rename numbers to be array, or rename array to be numbers.
• Mar 29, 2011, 18:46
minusten
I'm not sure what you mean?
• Mar 29, 2011, 18:49
paul_wilkins
Quote:

Originally Posted by minusten
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.
• Mar 29, 2011, 19:00
minusten
Quote:

Originally Posted by paul_wilkins
So what will you do. Will you rename numbers to be array, or rename array to be numbers.

what would be the two results of the two scenarios:confused:
• Mar 29, 2011, 19:03
paul_wilkins
Quote:

Originally Posted by minusten
what would be 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.
• Mar 29, 2011, 19:09
minusten
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]+"\n";
• Mar 29, 2011, 19:15
minusten
Quote:

Originally Posted by paul_wilkins
Here's what is happening.

When you click on Calculate, it runs this code:

Code:

```sum(numbers); mean(numbers);```
So, where is the numbers variable coming from?
Currently it's undefined.

I just got it:D well for the moment my fuctions are looking for (numbers) not (array):eek:
• Mar 29, 2011, 19:17
paul_wilkins
Quote:

Originally Posted by minusten
that makes sense, like this?

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.
• Mar 29, 2011, 19:28
minusten
:DThanks so much! I been going crazy with this, I really appreciate your help :D

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 < array.length; i++)

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

}
• Mar 29, 2011, 19:41
paul_wilkins
Quote:

Originally Posted by minusten
This (val) part in blue-Is it correct/necessary?

Without it, it would be kind of hard for the function to know what number to add, so yes, it is necessary.
• Mar 29, 2011, 19:43
transio
For crying out loud...
Code html:

```<html> <head> <title>Aggregates</title>   <script type="text/javascript">   var numbers = new Array();   function insert(input) { var newNumber = input.value; input.value = ""; if (isNan(newNumber)) { window.alert("Invalid number entered!"); return; } 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(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; return total; }   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>     </head>   <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>```
• Mar 29, 2011, 19:51
paul_wilkins
Quote:

Originally Posted by transio
For crying out loud...

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.
• Mar 29, 2011, 19:58
transio
Quote:

Originally Posted by paul_wilkins
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...
• Mar 29, 2011, 20:03
transio
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.
• Mar 29, 2011, 20:17
paul_wilkins
Quote:

Originally Posted by transio
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
• Mar 29, 2011, 21:48
minusten
thanks paul is there a way i can pm you on this forum??:)
• Mar 29, 2011, 21:52
paul_wilkins
Quote:

Originally Posted by minusten
thanks paul is there a way i can pm you on this forum??:)

Yes indeed. Click on my name for a list of options.