# calculation

• 01-11-2013, 07:15 AM
princessMary
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
• 01-11-2013, 10:42 AM
JMRKER
Can you provide a sample of what you have attempted thus far?
• 01-11-2013, 01:18 PM
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>```
• 01-18-2013, 05:01 PM
jalarie
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>```
Quote:

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

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09437 seconds
• Memory Usage 2,357KB
• Queries Executed 11 (?)
Template Usage (21):
• (3)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates