settingsAccountsettings
Menusettings

Q: Calculate the area and perimeter of a rectangle by given two sides

+2 votes

Write a javascript function that calculates the area and perimeter of a rectangle by given two sides.

The input comes as 2 numbers that are the lengths of the 2 sides of the rectangle (sideA and sideB)

Examples:

Input:
2 2

Output:
4
8


Input:
1 3

Output:
3
8


Input:
2.5 3.14

Output:
7.85
11.28

The output should be printed to the console on two lines.

asked in JavaScript category by user ak47seo

1 Answer

+2 votes

The area of rectangle is: w*h

The perimeter of rectangle is: 2 * (w + h)

The js code is:

function areaRectangle(a, b) {
    console.log(a * b);//The area of rectangle is: w*h
    console.log(2 * (a + b));//The perimeter of rectangle is: 2 * (w + h)
}

areaRectangle(2.5, 3.14);

The multiplication operator will automatically coerce the input variables to numbers, so we can directly find the area of the rectangle by multiplying the two input elements.

The remaining operations are straightforward arithmetic and finally printing the two results (area and perimeter) to the console.

 

answered by user eiorgert
edited by user golearnweb
...