Skip to content

Commit 7256e53

Browse files
authored
algorithm class: circle (#1252)
1 parent f1ef64c commit 7256e53

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Geometry/Circle.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* This class represents a circle and can calculate it's perimeter and area
3+
* https://en.wikipedia.org/wiki/Circle
4+
* @constructor
5+
* @param {number} radius - The radius of the circule.
6+
*/
7+
export default class Circle {
8+
constructor (radius) {
9+
this.radius = radius
10+
}
11+
12+
perimeter = () => {
13+
return this.radius * 2 * Math.PI
14+
}
15+
16+
area = () => {
17+
return Math.pow(this.radius, 2) * Math.PI
18+
}
19+
}

Geometry/Test/Circle.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Circle from '../Circle'
2+
3+
const circle = new Circle(3)
4+
5+
test('The area of a circle with radius equal to 3', () => {
6+
expect(parseFloat(circle.area().toFixed(2))).toEqual(28.27)
7+
})
8+
9+
test('The perimeter of a circle with radius equal to 3', () => {
10+
expect(parseFloat(circle.perimeter().toFixed(2))).toEqual(18.85)
11+
})

0 commit comments

Comments
 (0)