www.webdeveloper.com
Results 1 to 8 of 8

Thread: Trying to get Area, and Volume of a Cylinder

  1. #1
    Join Date
    May 2012
    Posts
    8

    Trying to get Area, and Volume of a Cylinder

    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,

    <html>
    <head>
    <script>


    function Cylinder(radius) {


    this.radius = radius;
    this.volume = volume;

    this.surface_area = surface_area;
    //this.height = height;

    alert("radius1: " + this.radius);
    // alert("radius: " + radius);

    }



    function Cylheight(height){
    this.height = height;

    //return height;

    // alert("hight: " + this.height);
    }





    function volume() {


    var volume = Math.PI * (this.radius * this.radius) * this.height;
    alert("radius1: " + this.radius);
    alert("hight1: " + this.height);
    return volume;



    }




    function surface_area() {

    var surface_area = (Math.PI * 2) * (this.radius * this.radius) + (Math.PI * 2) * this.radius * this.height ;

    alert("radius2: " + this.radius);
    return surface_area;


    }


    </script>

    </head>

    <body>

    <h1>Volume, and Surface Area</h1>

    <script>



    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>

  2. #2
    Join Date
    May 2012
    Posts
    8
    I am getting undefined for the height,

  3. #3
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    After spending around 15 mins to debug your code,
    I was only able to make out that your code is a total mess!

    The only possible solution to me is to re-code your code!
    "It will never rain roses: when we want to have more roses, we must plant more roses." - George Eliot

  4. #4
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,460
    Hi there RustyC,

    and a warm welcome to these forums.

    Try it like this...
    Code:
    <!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>
    
    coothead

  5. #5
    Join Date
    May 2012
    Posts
    8
    Thanks Coothead, As you can tell I am new to Javascript. I appreciate you taking the time to help.

    Rusty

  6. #6
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,460
    No problem, you're very welcome.
    coothead

  7. #7
    Join Date
    Feb 2006
    Posts
    2,927
    Another way, using the constructor-

    Code:
    <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>

  8. #8
    Join Date
    May 2012
    Posts
    8
    Thanks Mrhoo. I appreciate the help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles