Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-05-2011, 01:56 PM   PM User | #1
university
New Coder

 
Join Date: Jul 2011
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
university has a little shameless behaviour in the past
how to write javascript code for this function....

write a function that when called will ask the user for a series of numbers to be entered 1 at a time and then calculates their sum,their average,and their average rounded up then output the results.

for example the average of a series of numbers is found by adding the numbers together and dividing by the amount of numbers entered (so the average of 2,5,1 and 7 is equal to (2+5+1+7)/4=15/4=3.75).
university is offline   Reply With Quote
Old 07-05-2011, 02:48 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Please show us what you have already
devnull69 is offline   Reply With Quote
Old 07-05-2011, 03:29 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Blatant homework!! See Forum Rule 1.5

It is not really in your best interests that others do all or most of your homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. Finally, there is a limit to the number of times that you can take your pitcher to this well!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 07-05-2011, 04:04 PM   PM User | #4
Hamza7
New Coder

 
Join Date: Jun 2011
Location: Algeirs,Algeria
Posts: 43
Thanks: 5
Thanked 3 Times in 3 Posts
Hamza7 is an unknown quantity at this point
Here is the code, I made my best efforts to make it short and simplified as much as possible:

Code:
/*----------------------------------Setting section-------------------------------------------*/
var Request = {};
//Set the message to dispaly:
Request.message = "Enter a series of numbers to calculate the average (comma separated):";
//Set the (optional) default example:
Request.example = "2,5,1,7";
//Set parsing type: "float" (for numbers with floating decimal point) or "int" (for integers):
Request.parsingType = "int";
//Set rounding setting (an natural number, ex: 2, 3, 5, 8...etc):
Request.round = 2;
/*--------------------------------------Function section---------------------------------------*/
function parse(String,type){if(type=="float")return parseFloat(String); else if(type="int")return parseInt(String);}
//The main function:
function calculateAvg(){
var numSeries = prompt(Request.message,Request.example||"");
var numArray = numSeries.split(/\s*,\s*/);
var result = 0;
//Calculating the sum:
for (var num = 0;num<numArray.length; num++)result+=parse(numArray[num],Request.parsingType);
//Calculating the average:
result = result/numArray.length;
//Rounding the result:
result = result.toFixed(Request.round);
//Showing the result:
alert(result);
//Returning the value:
return result;}
You can also download the file from:
http://www.filesonic.com/file/137393...alculateAvg.js
Hamza7 is offline   Reply With Quote
Users who have thanked Hamza7 for this post:
university (07-06-2011)
Old 07-06-2011, 06:09 PM   PM User | #5
university
New Coder

 
Join Date: Jul 2011
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
university has a little shameless behaviour in the past
Quote:
Originally Posted by Philip M View Post
Blatant homework!! See Forum Rule 1.5

It is not really in your best interests that others do all or most of your homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. Finally, there is a limit to the number of times that you can take your pitcher to this well!
its not homework im new at this sort of stuff and so this summer am really trying to get the hang of it and understand it better for next year as im goin to be dong course init...and i know if i just copy something it will be of no use to me am asking questions because i really need help so came across this website so thought i cud get some help that is all!
university is offline   Reply With Quote
Old 07-06-2011, 07:51 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Once more, this forum is not a substitute for a student textbook, nor a free coding service. It aims to help people overcome specific problems with Javascript. Show us the code you yourself have written and we will be happy to correct or improve it. But you must make a genuine effort yourself.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:36 AM.


Advertisement
Log in to turn off these ads.