forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
algorithm class: circle (TheAlgorithms#1252)
- Loading branch information
1 parent
f1ef64c
commit 7256e53
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |