arrays & functions in loops

repeating random number script 3 times

Page 1 of 1

1 Replies - 2215 Views - Last Post: 17 April 2009 - 05:28 AM

#1 graphichill   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 17-February 09

arrays & functions in loops

Post icon  Posted 15 April 2009 - 08:24 AM

im currently learning javascript from sams teach yourself book and have a exercise that i cant get my head around:- Exercise: modify the random number script to run three times, calculating a total 15,000 random rumbers, and display separate totals for each set of 5,000 (you'll need to use another for loop that encloses most of the script. you start with this...

 <script language="JavasScript" type="text/javascript">
	   var total = 0;
	   
	  
	   var rannumber  = window.prompt ("how many random numbers do you want to generate");

	   for( var i=1; i<=rannumber; i++);{
	   num = Math.random();
	   total += num;
}
	  average = total/rannumber;
	  average = Math.round(average * 1000) / 1000;
	  
	  document.write("<h2>Average of " + rannumber + " numbers: " + average + "</h2>");
 </script>
  


This is my attempt using an array to try and store the result in and repeat it three times using a function... has anyone have any ideas?

 <script language="JavasScript" type="text/javascript">

	  var average = new Array(0);

	   var total = 0;
	   
	   function  averagenumbers(){
	   var rannumber  = window.prompt ("how many random numbers do you want to generate");
	   for( var i=1; i<=rannumber; i++);{
	   num = Math.random();
	   total += num;
	  average = total/rannumber;
	  average = Math.round(average * 1000) / 1000;
	  average = average[i]
	  }
	   }
	 
	 
		for(var i=1; i<=3; i++){
		averagenumbers();
alltogther = average[0] + average[1] + average[2];

}
  document.write("<h2>"Average of " + rannumber + " numbers: " + average + "</h2>");
 document.write("<h2>all togther: " + alltogther + "</h2>");

  </script>



Is This A Good Question/Topic? 0
  • +

Replies To: arrays & functions in loops

#2 Astano   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 09-July 07

Re: arrays & functions in loops

Posted 17 April 2009 - 05:28 AM

The code below should do what is needed. I have added comments to explain the steps but if you have any questions please ask.


<script language="JavasScript" type="text/javascript">

//declare the array that will hold the three averages you generate
var averageThree = new Array();

//the function to generate the average of a user specified number of numbers 
function  averagenumbers(){

	//prompt the user for the number of numbers to average
	var rannumber  = prompt("How many random numbers do you want to generate?");

	//declare the total variable and give it the initial value of 0
	var total = 0;

	document.write("<p><b>Average of :</b><br>")

	//the bit that generates the average number and keeps a running total, also writes the number generated to the page
	for( var i=1; i<=rannumber; i++){

		num = Math.random();
		total += num;
		document.write(num + "<br>");
	}

	//works out the average of the numbers generate and prints it to the page - this method converts the number to a string so we have to use parseFloat later to convert back
	average = (total/rannumber).toFixed(3);
	document.write("<b>equals " + average + "</b></p>");

	//tell the function to return the value of average - this will be useful later
	return parseFloat(average);
}

//initialises the totalTotal variable with a value of 0
var totalTotal = 0;

//for loop to assign the value returned for average from the function averagenumbers() to the array averageThree()
for(var i=0; i<=2; i++){

	//this does the actual assignment, because you told the function to return a value when you code as below the value returned is passed to the variable
	averageThree[i] = averagenumbers();

	//keeps a running total of all values in the array
	totalTotal += (averageThree[i]);

}

//works out the average of all values in the array
averageTotal = (totalTotal/3).toFixed(3);

//writes the average
document.write("Average of three previous averages: " + averageTotal);


  </script>



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1