Skip to content

Commit f1ef64c

Browse files
authored
algorithm class: cone (TheAlgorithms#1253)
1 parent c5101e3 commit f1ef64c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Geometry/Cone.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This class represents a circular cone and can calculate its volume and surface area
3+
* https://en.wikipedia.org/wiki/Cone
4+
* @constructor
5+
* @param {number} baseRadius - The radius of the base of the cone.
6+
* @param {number} height - The height of the cone
7+
*/
8+
export default class Cone {
9+
constructor (baseRadius, height) {
10+
this.baseRadius = baseRadius
11+
this.height = height
12+
}
13+
14+
baseArea = () => {
15+
return Math.pow(this.baseRadius, 2) * Math.PI
16+
}
17+
18+
volume = () => {
19+
return this.baseArea() * this.height * 1 / 3
20+
}
21+
22+
surfaceArea = () => {
23+
return this.baseArea() + Math.PI * this.baseRadius * Math.sqrt(Math.pow(this.baseRadius, 2) + Math.pow(this.height, 2))
24+
}
25+
}

Geometry/Test/Cone.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Cone from '../Cone'
2+
3+
const cone = new Cone(3, 5)
4+
5+
test('The Volume of a cone with base radius equal to 3 and height equal to 5', () => {
6+
expect(parseFloat(cone.volume().toFixed(2))).toEqual(47.12)
7+
})
8+
9+
test('The Surface Area of a cone with base radius equal to 3 and height equal to 5', () => {
10+
expect(parseFloat(cone.surfaceArea().toFixed(2))).toEqual(83.23)
11+
})

0 commit comments

Comments
 (0)