Skip to content

Commit 863185d

Browse files
authored
algorithm class: pyramid (TheAlgorithms#1254)
* Added pyramid * Removed * 1 * Change class name
1 parent 014a38b commit 863185d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Geometry/Pyramid.js

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

Geometry/Test/Pyramid.test.js

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy