I am having trouble trying to get the following to work. It will pass the radius, but not the height. Any help you can be would be greately appreciated,
var rad = parseInt(prompt("Enter the radius of the cylinder: "));
var hgt = parseInt(prompt("Enter the height of the cylinder: "));
var myCylinder = new Cylinder(rad);
var myCylheight = new Cylheight(hgt);
alert("The cylinder's volume is " + myCylinder.volume() + " units");
alert("The cylinder's Surface Area is " + myCylinder.surface_area() + " units");
</script>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title></title>
<script type="text/javascript">
function init(){
var rad,hgt;
while(isNaN(rad)) {
rad=parseFloat(prompt('Enter the radius of the cylinder: '));
if(isNaN(rad)) {
alert('Please enter a number');
}
}
while(isNaN(hgt)) {
hgt=parseFloat(prompt('Enter the height of the cylinder: '))
if(isNaN(hgt)) {
alert('Please enter a number');
}
}
area=2*Math.PI*rad*(rad+hgt);
volume=Math.PI*rad*rad*hgt;
document.getElementById('result').innerHTML=
'The cylinder\'s Surface Area is '+area.toFixed(2)+' square units.<br>'+
'The cylinder\'s volume is '+volume.toFixed(2)+' cubic units.';
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<h1>Volume, and Surface Area</h1>
<div id="result"></div>
</body>
</html>
<html>
<head>
</head>
<body>
<h1>Volume, and Surface Area</h1>
<script>
function Cylinder(r,h){
this.radius= r;
this.height= h;
}
Cylinder.prototype.getVolume=function(){
var r= this.radius, h= this.height, pi= Math.PI;
return pi*r*r*h;
}
Cylinder.prototype.getSurface=function(){
var r= this.radius, h= this.height, pi2= Math.PI*2;
return pi2*r*r+ pi2*r*h;
}
var rad= parseInt(prompt("Enter the radius of the cylinder: ",""));
var hgt= parseInt(prompt("Enter the height of the cylinder: ",""));
var units= ' units';
var c1=new Cylinder(rad,hgt);
alert("The volume is " + c1.getVolume().toFixed(4)+
" cubic "+units+"\nThe Surface Area is " + c1.getSurface().toFixed(4)+" square "+units);
</script>
</body>
</html>
Bookmarks