...

View Full Version : How to calculate the area of a Rectangle



Wardy118
08-01-2006, 01:57 PM
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>

vwphillips
08-01-2006, 02:15 PM
<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>

Wardy118
08-01-2006, 02:26 PM
thanks for such a quick response. that has worked wonders.

Cheers once again Vic

Wardy

Wardy118
08-01-2006, 04:15 PM
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

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

vwphillips
08-01-2006, 05:51 PM
document.write may only be used a the page is first rendered


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

Wardy118
08-01-2006, 06:45 PM
Thanks for your help vic



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum