The Math object

The Math object is particularly useful for creating online demonstrations. Here we use the Math object to determine properties of a circle and sphere given the radius, displaying the results on a clear page.


The following code goes in your document where you want the controls to appear:
<form name="mathForm">
  <input type="button" value="Go!" onClick="goForIt()">
</form>

You also need to put the following code in the header of the document (somewhere between <HEAD> and </HEAD>):
<script language="JavaScript"><!--
function goForIt(){
  text1=prompt('Enter the desired radius below','5');	//prompts the user for a number
  var radius=parseFloat(text1);				//interpret the input string as an integer
  var circ=Math.PI*radius*2;				//calculate circumference
  var area=Math.PI*Math.pow(radius,3);			//calculate area
  var vol=Math.PI*Math.pow(radius,3)*4/3;		//calculate volume
  var textToDisplay='For a radius '+ radius +'cm:<br>';
  textToDisplay+='The circumference of the circle is '+ circ +'cm<br>';	//concatenating strings...
  textToDisplay+='The area of the circle is '+ area +'square cm<br>';	//makes it much easier
  textToDisplay+='The volume of the sphere is '+ vol +'cubic cm<br>';	//to read your script
  document.write(textToDisplay);	//displays output, clearing the current page in the process
}
// --></script>

The code is implemented below: