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,860
    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,860
    No problem, you're very welcome.
    coothead

  7. #7
    Join Date
    Feb 2006
    Posts
    2,930
    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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.41510 seconds
  • Memory Usage 2,915KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates