Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to calculate the area of a Rectangle

    I have been looking at this for a few days now but cant see where i'm going wrong. Basically i want the program to ask the user to input the length and width of 4 rectangles, calcualte the area of each and then store the information in the array which will then be shown to the user.

    Here is my code so far which only asks the user for 1 rectangle.

    <TITLE>test</TITLE>

    <SCRIPT type="text/javascript">

    var RectangleLength;
    var RectangleWidth;

    function test(){


    RectangleLength = window.prompt('Please enter the length of the rectangle in centimetres','');
    RectangleLength=parseFloat(RectangleLength);

    RectangleWidth = window.prompt('Please enter the width of the rectangle in centimetres','');
    RectangleWidth=parseFloat(RectangleWidth);

    document.write('The area of the 'Rectangle' is ' +RectangleLength * RectangleWidth+ ' square centimetres <br>');

    }


    </SCRIPT>
    </HEAD>
    <BODY onload="test()">

    <div id="display"></div>

    </BODY>
    </HTML>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <TITLE>test</TITLE>
    
    <SCRIPT type="text/javascript">
    
    var RectangleLength;
    var RectangleWidth;
    var Ary=[];
    function test(){
    
    RectangleLength = window.prompt('Please enter the length of the rectangle in centimetres','');
    RectangleLength=parseFloat(RectangleLength);
    
    RectangleWidth = window.prompt('Please enter the width of the rectangle in centimetres','');
    RectangleWidth=parseFloat(RectangleWidth);
    
    Ary.push(RectangleWidth*RectangleLength);
    var html='';
    for (var zxc0=0;zxc0<Ary.length;zxc0++){
     html+='The area of the Rectangle '+(zxc0+1)+' is ' +Ary[zxc0]+ ' square centimetres <br>'
    }
    
    document.getElementById('display').innerHTML=html;
    if (Ary.length<4){ test(); }
    
    }
    
    
    </SCRIPT>
    </HEAD>
    <BODY onload="test()">
    
    <div id="display"></div>
    
    </BODY>
    </HTML>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thanks for such a quick response. that has worked wonders.

    Cheers once again Vic

    Wardy

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I want to do a similar program to the rectangle one i mentioned in this thread. Basically i want a program which allows the user to find out the area of 2 shapes being Rectangle and Circle. The User should type 'r' for rectangle and 'c' for circle. The user will be able to input 4 dimensions which will then be stored into an array. I want the output to look something like this:

    Here are the shapes you entered and their areas

    1. Rectangle..............Area 3 Square Centimetres
    2. Circle....................Area 12.33 Square Centimetres
    3. Rectangle..............Area 34.55 Square Centimetres
    4. Circle.....................Area 50.23 Square Centimetres

    So far my program prompts the user to enter 'r' or 'c' and asks for the legth and width but it shows the ouput on the screen as it happens. what i want to happen is it to be stored and then shown all together at the end of the 4 chosen dimensions and shapes. I also want numbers to be included in the ouput which i can't seem to get on my program so far.

    Here is my code so far:

    <HTML>
    <HEAD>
    <TITLE>4 Dimensions</TITLE>

    <SCRIPT type="text/javascript">

    var shapeArray = new Array (4);
    var RectangleLength;
    var RectangleWidth;
    var sum1;
    var sum2;
    var circle;

    function test(){

    for (var i= 0; i< shapeArray.length; i++){

    shape=window.prompt('Please enter shape "r" for rectangle or "c" for Circle');


    if(shape=='r'){

    RectangleLength = window.prompt('Please enter the length of the rectangle in centimetres','');
    RectangleLength=parseFloat(RectangleLength);

    RectangleWidth = window.prompt('Please enter the width of the rectangle in centimetres','');
    RectangleWidth=parseFloat(RectangleWidth);
    shapeArray[i]='Rectangle..............................Area ' +RectangleLength * RectangleWidth+ ' square centimetres <br>'

    }

    if (shape=='c'){
    circle = window.prompt('Please enter the radius in centimetres','');
    circle=parseFloat(circle);

    shapeArray[i]='Circle.....................................Area '+circle * circle * 3.142+' square centimetres<br>'

    }

    document.getElementById("display").innerHTML+=shapeArray[i]

    }

    }


    </SCRIPT>
    </HEAD>
    <BODY onload="test()">

    <div id="display"></div>

    </BODY>
    </HTML>

    Any ideas where i'm going wrong?

    Thanks
    wardy

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I persume in oder to get the desired output of:

    Here are the shapes you entered and their areas

    1. Rectangle..............Area 3 Square Centimetres
    2. Circle....................Area 12.33 Square Centimetres
    3. Rectangle..............Area 34.55 Square Centimetres
    4. Circle.....................Area 50.23 Square Centimetres

    I would need to have a line saying something like document.write('Here are the shapes you entered and their areas'), but wherever i put this in the program it just has this as the ouput and doesnt allow the user to enter dimensions. It basically ignores the shapes and dimensions which follow.

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    document.write may only be used a the page is first rendered
    Code:
    <TITLE>test</TITLE>
    
    <SCRIPT type="text/javascript">
    
    var Ary=[];
    
    function test(){
     var type=window.prompt('Rectangle - enter R\nCircle - enter C','C');
     if (type=='R'){
      var RectangleLength = window.prompt('Please enter the length of the rectangle in centimetres',' ');
      var RectangleWidth = window.prompt('Please enter the width of the rectangle in centimetres',' ');
      if (!isNaN(RectangleLength)&&!isNaN(RectangleWidth)){ Ary.push(RectangleWidth*RectangleLength); }
     }
     if (type=='C'){
      var rad=window.prompt('Please enter the radius of the circle','');
      area=Math.PI*rad*rad;
      if (!isNaN(rad)){ Ary.push(Math.PI*rad*rad); }
     }
     var html='';
     for (var zxc0=0;zxc0<Ary.length;zxc0++){
      html+='The area of the '+((type=="R")?"Rectangle ":"Circle ")+(zxc0+1)+' is ' +(Ary[zxc0].toFixed(2))+ ' square centimetres.<br>';
     }
     document.getElementById('display').innerHTML=html;
     if (Ary.length<4){ test(); }
    }
    
    
    </SCRIPT>
    </HEAD>
    <BODY onload="test()">
    
    <div id="display"></div>
    
    </BODY>
    </HTML>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #7
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for your help vic


  •  

    Posting Permissions

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