1. Registered User
Join Date
Jan 2013
Posts
1

calculation

Hi all,

I am new in the web developing field and I'd like to ask you about a script I have to do.
I need to create a form in JavaScript that will have a text field and a button. You'll import several numbers in the field, separated with comma. When you press the button will be appeared the following
1)the minimum number
2)the maximum number
3)the average of the numbers

I'd appreciate If someone could help me here..
thank you so much!

Mary

2. Can you provide a sample of what you have attempted thus far?

3. Code:
```<input id="data">
<input type="button" value='compute' onclick="calc()">

<script>
function calc(){
var r=data.value.split(/\s*,\s*/).map(Number);
var mx=Math.max.apply(0, r);
var mn=Math.min.apply(0, r);
var avg=r.reduce(function(a,b){return a+b;}) / r.length;
}
</script>```

4. Registered User
Join Date
Nov 2002
Location
Flint, Michigan, USA
Posts
652
Code:
```<form>
<input id="data">
<input type="button" value='compute' onclick="calc()">
</form>

<script>
function calc(){
var Idata=document.forms[0].data.value;
var Items=Idata.split(/,/);
var Min=999, Max=-999, Total=0, Count=0;
for (var i=0; i< Items.length; i++) {
Item=Items[i];
Count++;
Total=Total*1+Item*1;
if (Item*1 < Min*1) { Min=Item; }
if (Item*1 > Max*1) { Max=Item; }
}
Average=Total/Count;
}
</script>```
Originally Posted by rnd me
Code:
```<input id="data">
<input type="button" value='compute' onclick="calc()">

<script>
function calc(){
var r=data.value.split(/\s*,\s*/).map(Number);
var mx=Math.max.apply(0, r);
var mn=Math.min.apply(0, r);
var avg=r.reduce(function(a,b){return a+b;}) / r.length;
}
</script>```