PDA

View Full Version : Rectangle Part 2 - This time converting centimetres to Inches

Wardy118
08-01-2006, 07:45 PM
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>
<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=parseFloat(circle);

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

}

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

}

}

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

</BODY>
</HTML>

vwphillips
08-01-2006, 08:02 PM
/2.54

Wardy118
08-01-2006, 09:16 PM
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>
<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=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=parseFloat(circle);

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

}

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

}

}

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

</BODY>
</HTML>

Wardy118
08-01-2006, 09:22 PM
Anybody know where i'm going wrong?

Wardy118
08-01-2006, 10:05 PM
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.

Wardy118
08-01-2006, 10:55 PM
If anyone could let me know where i'm going wrong that would be great, i depseratley need to finish the program tonight.

vwphillips
08-01-2006, 11:04 PM
<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 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>

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

</BODY>
</HTML>

boeing747fp
08-01-2006, 11:05 PM
you defined function convertToCentimetres() but you are not calling it from anywhere.

Wardy118
08-01-2006, 11:18 PM
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.

vwphillips
08-02-2006, 11:24 AM
realised that this is prob homework

you have more than enough to complete this yourself if you try