From 014a38b2d42968805552dc4b6d355ac90bb859f7 Mon Sep 17 00:00:00 2001 From: Lcvieira2001 <114815013+Lcvieira2001@users.noreply.github.com> Date: Mon, 31 Oct 2022 13:39:41 -0300 Subject: [PATCH] algorithm: volume of sphere (#1249) * Added sphere volume * Fixed indentation * Added PR Suggestions --- Geometry/Sphere.js | 19 +++++++++++++++++++ Geometry/Test/Sphere.test.js | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 Geometry/Sphere.js create mode 100644 Geometry/Test/Sphere.test.js diff --git a/Geometry/Sphere.js b/Geometry/Sphere.js new file mode 100644 index 000000000..82f539b91 --- /dev/null +++ b/Geometry/Sphere.js @@ -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 + } +} diff --git a/Geometry/Test/Sphere.test.js b/Geometry/Test/Sphere.test.js new file mode 100644 index 000000000..18f8333a7 --- /dev/null +++ b/Geometry/Test/Sphere.test.js @@ -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) +})