/    Sign up×
Community /Pin to ProfileBookmark

average of user-defined numbers

i need a webpage that has a button for the user to input some numbers (but i dont know how many) and i need to calculate the minimum, maximum, sum, and average of these numbers.
i want the button to make a popup that asks the user for a number and keeps doing so until the user types “stop” and then have javascript store the numbers (maybe into an array) in order to figure out the max, min, sum, and average.

i’m pretty new to javascript so i’m wondering how exactly i would go about doing this… thanks for your help

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMar 11.2006 — This seems like a school assignment. Is it? If so, then if you don't understand how to do your school assignments, you should be asking your teacher for this kind of help. You will benefit in the long run.

If not an assignment, then post some JavaScript and HTML code to show what you're working on. Thanks.
Copy linkTweet thisAlerts:
@celeritasauthorMar 12.2006 — i guess it does sound like a school assignment but its not (i dont even go to school anymore ! haha)

anyway, here's what i have so far.. but i'm struggling with how it loops. a[a.length] = prompt(...... makes sense to me but its not continuing to prompt the user.. i guess i dont understand how to continually input some variable as the next part of the array.

thanks again for helping a humble beginner understand this !


<script type="text/javascript">

var a = new Array();

var x, sum;

a[a.length] = prompt("enter a number","");

sum = 0;

for (x=0; x < a[a.length]; a[a.length] = prompt("new number",""))

{

a[x] = x;

sum = sum + a[x];

}

var avg = sum / a[a.length]

document.write("The average is "+avg+"<br>");

document.write("The sum is "+ sum +"<br>");

</script>
Copy linkTweet thisAlerts:
@domiereavronMar 12.2006 — I would probably do a do while loop, exiting on the condition that you get a NAN

use parseInt, if the user does not know the total of the number

or, I would just take a prompt asking how many numbers in total

keeping count with a variable, count=count+1

total=total + a[count]

then when its all said and done

avg=total/count

document.write("the average is "+ avg+"<BR>");

document.write("the total is "+total+"<BR>");

its much more simplified
Copy linkTweet thisAlerts:
@phpnoviceMar 12.2006 — Something like this: (PHP bounding tags for coloring only.)
[code=php]
var sum = x = 0, a = new Array();
var txt = msg = "Enter a number:";
while (x!=null) {
x = prompt(txt, x);
if (x!=null) {
x = Number(x);
if(isNaN(x)) txt = "Enter numbers only:";
else {
txt = msg;
a[a.length] = x;
sum += x;
x = 0;
}
}
}
document.write("The average is "+(sum/a.length)+"<br>");
document.write("The sum is "+ sum +"<br>");
[/code]
Copy linkTweet thisAlerts:
@celeritasauthorMar 16.2006 — phpnovice, that looks good. and i appreciate your assistance, but is there a way so that "0" doesnt appear in the textbox of the popup and also for the popups to stop when the user types "stop" instead of clicking the cancel button ?

but i want to incorporate the sum and average into the something like the following which uses max and min values too... also, how do i continually create an array (increasing the index using i++ and using prompt so that [I]x[/I] would be the value of the increasing index and also be what the user inputs and probably use [I]while (x != "stop")[/I]... i feel like i have the code but just have trouble creating the array that the code would apply to

[I]

var a=new Array();

for (var i=0; i<a.length; i++){

var minindex=i;

var maxindex=i;

for (var j=i+1; j<a.length; j++){

if (a[minindex]>a[j]){

minindex=j;

}

else if (a[maxindex]<a[j]){

maxindex=j;

}

}

alert("the minimum is " + a[minindex] + "nthe maximum is " + a[maxindex])[/I]


thanks for your help everyone!!
Copy linkTweet thisAlerts:
@phpnoviceMar 16.2006 — Sure, you can test for both null and stop:

(x!=null && x!='stop')
×

Success!

Help @celeritas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...