/    Sign up×
Community /Pin to ProfileBookmark

averaging from an array

ok… I know you all have seen your fare share of posts about averaging numbers. even ones about arrays.

well, here is another one…

[obvious]
average = sum / count
[/obvious]

my JS has an array that has no determined length (until you press the Q — for quit)… so, I am trying to use .length as the count (to no apparent success). is this even logical?

here is the one I have been working on all day;

[code]
<script language=”javascript”>
<!–
var count;
var len = parseInt(prompt(“how many figures in your sales record? :”,””));
if(isNaN(len)){
alert(“you need to enter a number, please RETRY”);
location.reload();}
var salesData = new Array(len);
document.write(“all sales figures: &nbsp;&nbsp;&nbsp;”);

for(count=0;count<len;count++)
{
salesData[count] = prompt(“enter sales figures ‘Q to quit’: $”,””);
if(salesData[count]==”Q”){delete salesData[count];
break};
if(isNaN(salesData[count])){delete salesData[count];
alert(“you entered non-numeric data: F5 to restart”);
salesData[count]=salesData[count]-0;
break};
document.write(“$”+salesData[count]+”&nbsp;&nbsp;&nbsp;”);
}

document.write(“<p>maximum sales figure: $”);
var lngMax = eval(“Math.max(” + salesData.join(“,”) + “)”)
document.write(lngMax);

document.write(“<p>minimum sales figure: $”);
var lngMin = eval(“Math.min(” + salesData.join(“,”) + “)”)
document.write(lngMin);

sum = 0;
for (x = 0; x < salesData.length; x++)
sum += parseFloat(salesData[x]);
document.write(“<P>total sales: $”+sum);

var average = sum / salesData[len].length;
document.write(“<P>average sales amount: $” + average);

//–></script>
[/code]

the averaging part is that last couple lines (as that is where I am stuck). I been trying this step-by-step, and its getting there…

can anyone point out to me what I might try to do to fix my averaging? at this point, it doesnt even write and I get the error ‘salesData[…].length’ is null or not an object

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@psuedocodeauthorMar 10.2005 — ok... so, maybe my problem is that I think things are more difficult than they are....

the solution was pretty easy once I backed off of it and looked from a different angle...


solution:
<i>
</i>sum = 0;
for (x = 0; x &lt; salesData.length; x++)
sum += parseFloat(salesData[x]);
document.write("&lt;P&gt;total sales: $"+sum);

document.write("&lt;br&gt;average sales amount: $" + sum / salesData.length);
×

Success!

Help @psuedocode 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...