1. ## 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="button" value="Reset" onClick="form.reset();" />
</form>

</body>

</html>

2. Code javascript:
```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!

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

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

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

7. I'm not sure what you mean?

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

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

10. Originally Posted by minusten
what would be the two scenarios?
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.

11. 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";

12. 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 well for the moment my fuctions are looking for (numbers) not (array)

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

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

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

}

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

16. For crying out loud...
Code html:
```<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]+"\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;
}

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

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

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

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

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

21. thanks paul is there a way i can pm you on this forum??

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