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: ”);
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]+” ”);
}
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>
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