Skip to content

Commit b5ee1f6

Browse files
committed
feat: add geometry rectangle
1 parent 9010481 commit b5ee1f6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Geometry/Rectangle.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* This class represents a rectangle and can calculate its perimeter and area
3+
* https://en.wikipedia.org/wiki/Rectangle
4+
* @constructor
5+
* @param {number} length - The length of the rectangle.
6+
* @param {number} breadth - The breadth of the rectangle.
7+
*/
8+
export default class Rectangle {
9+
constructor(length, breadth) {
10+
this.length = length
11+
this.breadth = breadth
12+
}
13+
14+
perimeter = () => {
15+
return 2 * (this.length + this.breadth)
16+
}
17+
18+
area = () => {
19+
return this.length * this.breadth
20+
}
21+
}

Geometry/Test/Rectangle.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Rectangle from '../Rectangle'
2+
3+
const rectangle = new Rectangle(3, 4)
4+
5+
test('The perimeter of rectangle with length equal to 3 and breadth equal to 4', () => {
6+
expect(parseFloat(rectangle.perimeter().toFixed(2))).toEqual(14.0)
7+
})
8+
9+
test('The area of rectangle with length equal to 3 and breadth equal to 4', () => {
10+
expect(parseFloat(rectangle.area().toFixed(2))).toEqual(12.0)
11+
})

0 commit comments

Comments
 (0)