diff --git a/Maths/Area.js b/Maths/Area.js new file mode 100644 index 0000000000..705de6a3ee --- /dev/null +++ b/Maths/Area.js @@ -0,0 +1,99 @@ +/* + Calculate the area of various shapes + + Calculate the Surface Area of a Cube. + Example: surfaceAreaCube(1) will return 6 + More about: https://en.wikipedia.org/wiki/Area#Surface_area + */ +const surfaceAreaCube = (sideLength) => { + validateNumericParam(sideLength, 'sideLength') + return (6.0 * sideLength ** 2.0) +} + +/* + Calculate the Surface Area of a Sphere. + Wikipedia reference: https://en.wikipedia.org/wiki/Sphere + return 4 * pi * r^2 +*/ +const surfaceAreaSphere = (radius) => { + validateNumericParam(radius, 'radius') + return (4.0 * Math.PI * radius ** 2.0) +} + +/* + Calculate the area of a rectangle + Wikipedia reference: https://en.wikipedia.org/wiki/Area#Quadrilateral_area + return width * length +*/ +const areaRectangle = (length, width) => { + validateNumericParam(length, 'Length') + validateNumericParam(width, 'Width') + return (width * length) +} + +/* + Calculate the area of a square +*/ +const areaSquare = (sideLength) => { + validateNumericParam(sideLength, 'side length') + return (sideLength ** 2) +} + +/* + Calculate the area of a triangle + Wikipedia reference: https://en.wikipedia.org/wiki/Area#Triangle_area + return base * height / 2 +*/ +const areaTriangle = (base, height) => { + validateNumericParam(base, 'Base') + validateNumericParam(height, 'Height') + return (base * height) / 2.0 +} + +/* + Calculate the area of a parallelogram + Wikipedia reference: https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles +*/ +const areaParallelogram = (base, height) => { + validateNumericParam(base, 'Base') + validateNumericParam(height, 'Height') + return (base * height) +} + +/* + Calculate the area of a trapezium +*/ +const areaTrapezium = (base1, base2, height) => { + validateNumericParam(base1, 'Base One') + validateNumericParam(base2, 'Base Two') + validateNumericParam(height, 'Height') + return 1.0 / 2.0 * (base1 + base2) * height +} + +/* + Calculate the area of a circle +*/ +const areaCircle = (radius) => { + validateNumericParam(radius, 'Radius') + return (Math.PI * radius ** 2) +} + +/* + Calculate the area of a rhombus + Wikipedia reference: https://en.wikipedia.org/wiki/Rhombus +*/ +const areaRhombus = (diagonal1, diagonal2) => { + validateNumericParam(diagonal1, 'diagonal one') + validateNumericParam(diagonal2, 'diagonal two') + return (1 / 2 * diagonal1 * diagonal2) +} + +const validateNumericParam = (param, paramName = 'param') => { + if (typeof param !== 'number') { + throw new TypeError('The ' + paramName + ' should be type Number') + } else if (param < 0) { + throw new Error('The ' + paramName + ' only accepts non-negative values') + } +} + +export { surfaceAreaCube, surfaceAreaSphere, areaRectangle, areaSquare, areaTriangle, areaParallelogram, areaTrapezium, areaCircle, areaRhombus } diff --git a/Maths/Area.test.js b/Maths/Area.test.js new file mode 100644 index 0000000000..838c48fd3c --- /dev/null +++ b/Maths/Area.test.js @@ -0,0 +1,99 @@ +import * as area from './Area' + +describe('Testing surfaceAreaCube calculations', () => { + it('with natural number', () => { + const surfaceAreaOfOne = area.surfaceAreaCube(1.2) + const surfaceAreaOfThree = area.surfaceAreaCube(3) + expect(surfaceAreaOfOne).toBe(8.64) + expect(surfaceAreaOfThree).toBe(54) + }) + it('with negative argument, expect throw', () => { + expect(() => area.surfaceAreaCube(-1)).toThrow() + }) + it('with non-numeric argument, expect throw', () => { + expect(() => area.surfaceAreaCube('199')).toThrow() + }) +}) +describe('Testing surfaceAreaSphere calculations', () => { + it('with correct value', () => { + const calculateArea = area.surfaceAreaSphere(5) + const expected = 314.1592653589793 + expect(calculateArea).toBe(expected) + }) + it('with negative value, expect throw', () => { + expect(() => area.surfaceAreaSphere(-1)).toThrow() + }) +}) +describe('Testing areaRectangle calculations', () => { + it('with correct args', () => { + const areaRectangle = area.areaRectangle(2.5, 2) + expect(areaRectangle).toBe(5.0) + }) + it('with incorrect args, expect throw', () => { + expect(() => area.areaRectangle(-1, 20)).toThrow() + expect(() => area.areaRectangle('1', 0)).toThrow() + expect(() => area.areaRectangle(23, -1)).toThrow() + expect(() => area.areaRectangle(23, 'zero')).toThrow() + }) +}) +describe('Testing areaSquare calculations', () => { + it('with correct args', () => { + const areaSquare = area.areaSquare(2.5) + expect(areaSquare).toBe(6.25) + }) + it('with incorrect side length, expect throw', () => { + expect(() => area.areaSquare(-1)).toThrow() + expect(() => area.areaSquare('zero')).toThrow() + }) +}) +describe('Testing areaTriangle calculations', () => { + it('with correct args', () => { + const areaTriangle = area.areaTriangle(1.66, 3.44) + expect(areaTriangle).toBe(2.8552) + }) + it('with incorrect base and height, expect throw', () => { + expect(() => area.areaTriangle(-1, 1)).toThrow() + expect(() => area.areaTriangle(9, 'zero')).toThrow() + }) +}) +describe('Testing areaParallelogram calculations', () => { + it('with correct args', () => { + const areaParallelogram = area.areaParallelogram(1.66, 3.44) + expect(areaParallelogram).toBe(5.7104) + }) + it('with incorrect base and height, expect throw', () => { + expect(() => area.areaParallelogram(-1, 1)).toThrow() + expect(() => area.areaParallelogram(9, 'zero')).toThrow() + }) +}) +describe('Testing areaTrapezium calculations', () => { + it('with correct args', () => { + const areaTrapezium = area.areaTrapezium(1.66, 2.41, 4.1) + expect(areaTrapezium).toBe(8.3435) + }) + it('with incorrect bases and height, expect throw', () => { + expect(() => area.areaTrapezium(-1, 1, 0)).toThrow() + expect(() => area.areaTrapezium(9, 'zero', 2)).toThrow() + expect(() => area.areaTrapezium(9, 1, 'seven')).toThrow() + }) +}) +describe('Testing areaCircle calculations', () => { + it('with correct args', () => { + const areaCircle = area.areaCircle(3.456) + expect(areaCircle).toBe(37.52298159254666) + }) + it('with incorrect diagonal, expect throw', () => { + expect(() => area.areaCircle(-1)).toThrow() + expect(() => area.areaCircle('zero')).toThrow() + }) +}) +describe('Testing areaRhombus calculations', () => { + it('with correct args', () => { + const areaRhombus = area.areaRhombus(2.5, 2.0) + expect(areaRhombus).toBe(2.5) + }) + it('with incorrect diagonals, expect throw', () => { + expect(() => area.areaRhombus(7, -1)).toThrow() + expect(() => area.areaRhombus('zero', 2)).toThrow() + }) +})