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
Can you provide a sample of what you have attempted thus far?
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;
alert( [mn, mx, avg] );
}
</script>
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;
alert('Minimum='+Min+' Maximum='+Max+' Total='+Total+' Count='+Count+' Average='+Average);
}
</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;
alert( [mn, mx, avg] );
}
</script>
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks