Question
4 Chapter 9 JavaScript: Functions
9.6 Write a complete JavaScript program to prompt the user for the radius of a sphere, and call function sphereVolume to calculate and display the volume of the sphere. Use the statement
volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );
to calculate the volume. The user should input the radius through an XHTML text field in a <form> and click an XHTML button to initiate the calculation.
ANS: The following solution calculates the volume of a sphere using the radius entered by the user.
1 <?xml version = "1.0" encoding = "utf-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Exercise 9.6: volume.html --> 6 <html xmlns = "http://www.w3.org/1999/xhtml"> 7 <head> 8 <title>Calculating Sphere Volumes</title> 9 <script type = "text/javascript">
10 <!-- 11 function displayVolume() 12 { 13 var inputField = document.getElementById( "radiusField" ); 14 var radius = parseFloat( inputField.value ); 15 var answerField = document.getElementById( "answer" ); 16 answerField.value = sphereVolume( radius ); 17 } // end function displayVolume 18 19 function sphereVolume( radius ) 20 { 21 return ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ); 22 } // end function sphereVolume 23 // --> 24 </script> 25 </head> 26 <body> 27 <form action = ""> 28 <div> 29 <label>Radius: 30 <input id = "radiusField" type = "text" /></label> 31 <input type = "button" value = "Calculate" 32 onclick = "displayVolume()" /> 33 <br /> 34 <label>Answer: 35 <input id = "answer" type = "text" /></label> 36 </div> 37 </form> 38 </body> 39 </html>
© 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Exercises 5
Exercises 9.7 Write a script that prompts the user for the radius of a circle, uses a function circleArea to calculate the area of the circle, and prints the area of the circle.
1 <?xml version = "1.0" encoding = "utf-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Exercise 9.7: Solution --> 6 <html xmlns = "http://www.w3.org/1999/xhtml"> 7 <head> 8 <title>Solution 9.7</title> 9 <script type = "text/javascript">
10 <!-- 11 function getArea() 12 { 13 var form = document.getElementById( "myForm" ); 14 var input = parseFloat( form.number.value ); 15 form.result.value = circleArea( input ); 16 } // end function getArea 17 18 function circleArea( radius ) 19 { 20 return Math.PI * radius * radius; 21 } // end function circleArea 22 // --> 23 </script> 24 </head> 25 <body> 26 <form id = "myForm" action = ""> 27 <table border = "1"> 28 <tr><td>Enter radius</td> 29 <td><input id = "number" type = "text" /> 30 </td> 31 <td><input type = "button" value = "Calculate" 32 onclick = "getArea()" /></td> 33 </tr> 34 <tr><td>Circle area</td> 35 <td><input id = "result" type = "text" /> 36 </td> 37 </tr> 38 </table>
© 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.