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 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Rectangle Part 2 - This time converting centimetres to Inches

    Right, I want to adapt a program i have just finished to allow the user to make a choice of the measurements they enter. Before with my original program the user has to enter the dimensions in centimetres, this time I want a program which requests the user to input the units they want to work in, either 'cm'(centimetres) or 'in' (inches). But the result of the area calculation the user recieves will still be in Square centimetres. which basically means if the user decides to use inches my program will have to convert the dimensions the user has inputed before the area calculation can take place.

    Here is my original code:

    <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;

    document.write('Here are the shapes you entered and their areas');

    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).toFixed(2)+ ' 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).toFixed(2)+' square centimetres<br>'

    }

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

    }

    }



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

    </BODY>
    </HTML>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    /2.54
    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
    Here's my new code but i just cant seem to get to grips with it. At the present moment in time the program works but not as it should. The conversion of inches to centimetres isn't working at all.

    <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;

    document.write('Here are the shapes you entered and their areas');

    measurement=window.prompt('Please enter the units you want to work in, "cm" for centimetres or "in" for inches');

    if(measurement=='in'){

    function convertToCentimetres(numberOfInches)
    /* converts centimetres to inches */
    {
    return numberOfInches * 2.54
    };

    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 inches','');
    RectangleLength=parseFloat(RectangleLength);

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

    }

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

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

    }
    }

    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).toFixed(2)+ ' 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).toFixed(2)+' square centimetres<br>'

    }

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

    }

    }



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

    </BODY>
    </HTML>

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Anybody know where i'm going wrong?

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    So far the beginning of the program is fine up to when it asks the user to type in 'cm' or 'in' but then when i type in 'in' it doesn't convert the inches back into centimetres to calculate the area.

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    If anyone could let me know where i'm going wrong that would be great, i depseratley need to finish the program tonight.

  • #7
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    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>';
      html+='The area of the '+((type=="R")?"Rectangle ":"Circle ")+(zxc0+1)+' is ' +((Ary[zxc0]/2.54).toFixed(2))+ ' square inches.<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/

  • #8
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    you defined function convertToCentimetres() but you are not calling it from anywhere.

  • #9
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That program Vic, is similar to the ouput intended. What i want is a window.prompt option at the begining of the program which asks the user to state whether they want to work in centimetres or inches. I have done this at the top of the program: measurement=window.prompt('Please enter the units you want to work in, "cm" for centimetres or "in" for inches');

    If the user selects 'cm' i want the program to continue as normal but if the user selects 'in' i want a function which converts the users inputted inches to centimetres. I want the output to always end with the area of the shapes being calculated in centimetres.

  • #10
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    realised that this is prob homework

    you have more than enough to complete this yourself if you try
    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/


  •  

    Posting Permissions

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