Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Math and Date Functions 1

Status
Not open for further replies.

tnbrwneyez

Programmer
Oct 2, 2004
19
US
I need to take the following script and prompt the user for the number of random numbers to generate. I also need to make the random number script run three times, calculating a total of 15,000 randon numbers and display separae total for each set of 5,000.


<html>
<head><title>Math Example</title>
</head>

<body>

<h1>Math Example</h1>
<script LANGUAGE="JavaScript" type="text/javascript">
total = 0;
for (i-1; i<=5000; i++){
num=Math.random();
total +=num;
window.status = "Generated" + i + " numbers. ";
}
average = total / 5000;
average=Math.round(average * 1000) / 1000;
document.write("<H2>Average of 5000 numbers: " + average + "</H2>");
</script>
</body>
</html>

I appreciate any help!

Thanks
Tammie
 
Code:
<script LANGUAGE="JavaScript" type="text/javascript">
function generate(limit) {
total = 0;
for (i-1; i<=limit; i++){
num=Math.random();
total +=num;
window.status = "Generated" + i + " numbers. ";
}
average = total / 5000;
average=Math.round(average * 1000) / 1000;
document.write("<H2>Average of 5000 numbers: " + average + "</H2>");
}

var lim = prompt("Generate how many numbers?","5000");
generate(lim);
generate(lim);
generate(lim);
</script>

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
When I open the page in the browser, it comes back with an error on line 11, stating that i is undefined. Any ideas what I did wrong?

Thanks for your help
 
for (i-1; i<=limit; i++){
should be
for (i[red]=[/red]1; i<=limit; i++){

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
Okay, now I need to take the following script and prompt the user for the number of random numbers to generate.

<html>
<head><title>Math Example</title>
</head>

<body>

<h1>Math Example</h1>
<script LANGUAGE="JavaScript" type="text/javascript">
total = 0;
for (i-1; i<=5000; i++){
num=Math.random();
total +=num;
window.status = "Generated" + i + " numbers. ";
}
average = total / 5000;
average=Math.round(average * 1000) / 1000;
document.write("<H2>Average of 5000 numbers: " + average + "</H2>");
</script>
</body>
</html>

Thanks!
 
see my first post. just copy+paste it over your current javascript.

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top