Skip to content

algorithm: volume of sphere #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Geometry/Sphere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* This class represents a sphere and can calculate its volume and surface area
* @constructor
* @param {number} radius - The radius of the sphere
* @see https://en.wikipedia.org/wiki/Sphere
*/
export default class Sphere {
constructor (radius) {
this.radius = radius
}

volume = () => {
return Math.pow(this.radius, 3) * Math.PI * 4 / 3
}

surfaceArea = () => {
return Math.pow(this.radius, 2) * Math.PI * 4
}
}
11 changes: 11 additions & 0 deletions Geometry/Test/Sphere.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Sphere from '../Sphere'

const sphere = new Sphere(3)

test('The Volume of a sphere with base radius equal to 3 and height equal to 5', () => {
expect(parseFloat(sphere.volume().toFixed(2))).toEqual(113.1)
})

test('The Surface Area of a sphere with base radius equal to 3 and height equal to 5', () => {
expect(parseFloat(sphere.surfaceArea().toFixed(2))).toEqual(113.1)
})