mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
algorithm class: circle (#1252)
This commit is contained in:
19
Geometry/Circle.js
Normal file
19
Geometry/Circle.js
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* This class represents a circle and can calculate it's perimeter and area
|
||||
* https://en.wikipedia.org/wiki/Circle
|
||||
* @constructor
|
||||
* @param {number} radius - The radius of the circule.
|
||||
*/
|
||||
export default class Circle {
|
||||
constructor (radius) {
|
||||
this.radius = radius
|
||||
}
|
||||
|
||||
perimeter = () => {
|
||||
return this.radius * 2 * Math.PI
|
||||
}
|
||||
|
||||
area = () => {
|
||||
return Math.pow(this.radius, 2) * Math.PI
|
||||
}
|
||||
}
|
11
Geometry/Test/Circle.test.js
Normal file
11
Geometry/Test/Circle.test.js
Normal file
@ -0,0 +1,11 @@
|
||||
import Circle from '../Circle'
|
||||
|
||||
const circle = new Circle(3)
|
||||
|
||||
test('The area of a circle with radius equal to 3', () => {
|
||||
expect(parseFloat(circle.area().toFixed(2))).toEqual(28.27)
|
||||
})
|
||||
|
||||
test('The perimeter of a circle with radius equal to 3', () => {
|
||||
expect(parseFloat(circle.perimeter().toFixed(2))).toEqual(18.85)
|
||||
})
|
Reference in New Issue
Block a user