From 39319037334fceace696fa806678d3c3dfd884e2 Mon Sep 17 00:00:00 2001 From: leeyan44 <86589062+leeyan44@users.noreply.github.com> Date: Mon, 19 Jul 2021 22:13:47 +0800 Subject: [PATCH 1/5] create a Volume.js in the math file Volume.js in the math file is to calculate the volume of some shapes such as Cuboid, Cube, Cone, Pyramid, Cylinder, Triangular Prism, Pentagonal Prism, Sphere and Hemisphere. --- Maths/Volume.js | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Maths/Volume.js diff --git a/Maths/Volume.js b/Maths/Volume.js new file mode 100644 index 000000000..ae65925a3 --- /dev/null +++ b/Maths/Volume.js @@ -0,0 +1,114 @@ +/* + Calculate the volume of the shapes + + Volume for Cuboid + Volume for Cube + Volume for Cone + Volume for Pyramid + Volume for Cylinder + Volume for Triangular Prism + Volume for Pentagonal Prism + Volume for Sphere + Volume for Hemisphere + */ + +/* + Calculate the volume for a Cuboid + return width * length * height +*/ +const volCuboid = (width, length, height) => { + isNumber(width, 'Width') + isNumber(length, 'Length') + isNumber(height, 'Height') + return (width * length * height) +} + +/* + Calculate the volume for a Cube + return length * length * length +*/ +const volCube = (length) => { + isNumber(length, 'Length') + return (length ** 3) +} + +/* + Calculate the volume for a Cone + return PI * radius^2 * height/3 +*/ +const volCone = (radius, height) => { + isNumber(radius, 'Radius') + isNumber(height, 'Height') + return (Math.PI * radius ** 2 * height/3) +} + +/* + Calculate the volume for a Pyramid + return (baseLength * baseWidth * height) / 3 +*/ +const volPyramid = (baseLength, baseWidth, height) => { + isNumber(baseLength, 'BaseLength') + isNumber(baseWidth, 'BaseWidth') + isNumber(height, 'Height') + return (baseLength * baseWidth * height) / 3 +} + +/* + Calculate the volume for a Cylinder + return PI * radius^2 * height +*/ +const volCylinder = (radius, height) => { + isNumber(radius, 'Radius') + isNumber(height, 'Height') + return (Math.PI * radius ** 2 * height) +} + +/* + Calculate the volume for a Triangular Prism + return 1 / 2 * baseLengthTriangle * heightTriangle * height +*/ +const volTriangularPrism = (baseLengthTriangle, heightTriangle, height) => { + isNumber(baseLengthTriangle, 'BaseLengthTriangle') + isNumber(heightTriangle, 'HeightTriangle') + isNumber(height, 'Height') + return (1 / 2 * baseLengthTriangle * heightTriangle * height) +} + +/* + Calculate the volume for a Pentagonal Prism + return 5/2 * pentagonalLength * pentagonalBaseLength * height +*/ +const volPentagonalPrism = (pentagonalLength, pentagonalBaseLength, height) => { + isNumber(pentagonalLength, 'PentagonalLength') + isNumber(pentagonalBaseLength, 'PentagonalBaseLength') + isNumber(height, 'Height') + return (5/2 * pentagonalLength * pentagonalBaseLength * height) +} + +/* + Calculate the volume for a Sphere + return 4/3 * PI * radius^3 +*/ +const volSphere = (radius) => { + isNumber(radius, 'Radius') + return (4/3 * PI * radius ** 3) +} + +/* + Calculate the volume for a Hemisphere + return (2 * PI * radius^3)/3 +*/ +const volHemisphere = (radius) => { + isNumber(radius, 'Radius') + return (2 * PI * radius ** 3)/3 +} + +const isNumber = (number, noName = 'number') => { + if (typeof number !== 'number') { + throw new TypeError('The ' + noName + ' should be Number type') + } else if (number < 0) { + throw new Error('The ' + noName + ' only accepts positive values') + } +} + +export {volCuboid, volCube, volCone, volPyramid, volCylinder, volTriangularPrism, volPentagonalPrism, volSphere, volHemisphere } From d7626e3b63308ef93125b5af4e732aa53b96b90b Mon Sep 17 00:00:00 2001 From: leeyan44 <86589062+leeyan44@users.noreply.github.com> Date: Mon, 19 Jul 2021 22:44:12 +0800 Subject: [PATCH 2/5] Update Volume.js --- Maths/Volume.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/Volume.js b/Maths/Volume.js index ae65925a3..c05117358 100644 --- a/Maths/Volume.js +++ b/Maths/Volume.js @@ -39,7 +39,7 @@ const volCube = (length) => { const volCone = (radius, height) => { isNumber(radius, 'Radius') isNumber(height, 'Height') - return (Math.PI * radius ** 2 * height/3) + return (Math.PI * radius ** 2 * height/3.0) } /* @@ -50,7 +50,7 @@ const volPyramid = (baseLength, baseWidth, height) => { isNumber(baseLength, 'BaseLength') isNumber(baseWidth, 'BaseWidth') isNumber(height, 'Height') - return (baseLength * baseWidth * height) / 3 + return (baseLength * baseWidth * height) / 3.0 } /* @@ -91,7 +91,7 @@ const volPentagonalPrism = (pentagonalLength, pentagonalBaseLength, height) => { */ const volSphere = (radius) => { isNumber(radius, 'Radius') - return (4/3 * PI * radius ** 3) + return (4/3 * Math.PI * radius ** 3) } /* @@ -100,7 +100,7 @@ const volSphere = (radius) => { */ const volHemisphere = (radius) => { isNumber(radius, 'Radius') - return (2 * PI * radius ** 3)/3 + return (2.0 * Math.PI * radius ** 3)/ 3.0 } const isNumber = (number, noName = 'number') => { From 9546f239f3fb8be1300c20440e7e655c066ec37b Mon Sep 17 00:00:00 2001 From: leeyan44 <86589062+leeyan44@users.noreply.github.com> Date: Tue, 20 Jul 2021 00:07:27 +0800 Subject: [PATCH 3/5] Update Volume.js --- Maths/Volume.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Maths/Volume.js b/Maths/Volume.js index c05117358..591728eff 100644 --- a/Maths/Volume.js +++ b/Maths/Volume.js @@ -14,6 +14,7 @@ /* Calculate the volume for a Cuboid + Reference: https://www.cuemath.com/measurement/volume-of-cuboid/ return width * length * height */ const volCuboid = (width, length, height) => { @@ -25,6 +26,7 @@ const volCuboid = (width, length, height) => { /* Calculate the volume for a Cube + Reference: https://www.cuemath.com/measurement/volume-of-cube/ return length * length * length */ const volCube = (length) => { @@ -34,6 +36,7 @@ const volCube = (length) => { /* Calculate the volume for a Cone + Reference: https://www.cuemath.com/measurement/volume-of-cone/ return PI * radius^2 * height/3 */ const volCone = (radius, height) => { @@ -44,6 +47,7 @@ const volCone = (radius, height) => { /* Calculate the volume for a Pyramid + Reference: https://www.cuemath.com/measurement/volume-of-pyramid/ return (baseLength * baseWidth * height) / 3 */ const volPyramid = (baseLength, baseWidth, height) => { @@ -55,6 +59,7 @@ const volPyramid = (baseLength, baseWidth, height) => { /* Calculate the volume for a Cylinder + Reference: https://www.cuemath.com/measurement/volume-of-cylinder/ return PI * radius^2 * height */ const volCylinder = (radius, height) => { @@ -65,6 +70,7 @@ const volCylinder = (radius, height) => { /* Calculate the volume for a Triangular Prism + Reference: http://lrd.kangan.edu.au/numbers/content/03_volume/04_page.htm return 1 / 2 * baseLengthTriangle * heightTriangle * height */ const volTriangularPrism = (baseLengthTriangle, heightTriangle, height) => { @@ -76,6 +82,7 @@ const volTriangularPrism = (baseLengthTriangle, heightTriangle, height) => { /* Calculate the volume for a Pentagonal Prism + Reference: https://www.cuemath.com/measurement/volume-of-pentagonal-prism/ return 5/2 * pentagonalLength * pentagonalBaseLength * height */ const volPentagonalPrism = (pentagonalLength, pentagonalBaseLength, height) => { @@ -87,6 +94,7 @@ const volPentagonalPrism = (pentagonalLength, pentagonalBaseLength, height) => { /* Calculate the volume for a Sphere + Reference: https://www.cuemath.com/measurement/volume-of-sphere/ return 4/3 * PI * radius^3 */ const volSphere = (radius) => { @@ -96,6 +104,7 @@ const volSphere = (radius) => { /* Calculate the volume for a Hemisphere + Reference: https://www.cuemath.com/measurement/volume-of-hemisphere/ return (2 * PI * radius^3)/3 */ const volHemisphere = (radius) => { From f52637f5466aa4e6e7706fdcd729c2cedec0b4b6 Mon Sep 17 00:00:00 2001 From: leeyan44 <86589062+leeyan44@users.noreply.github.com> Date: Tue, 20 Jul 2021 11:56:21 +0800 Subject: [PATCH 4/5] Update Maths/Volume.js Co-authored-by: Rak Laptudirm --- Maths/Volume.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/Volume.js b/Maths/Volume.js index 591728eff..b8a4f69f9 100644 --- a/Maths/Volume.js +++ b/Maths/Volume.js @@ -115,7 +115,7 @@ const volHemisphere = (radius) => { const isNumber = (number, noName = 'number') => { if (typeof number !== 'number') { throw new TypeError('The ' + noName + ' should be Number type') - } else if (number < 0) { + } else if (number < 0 || (!Number.isFinite(number))) { throw new Error('The ' + noName + ' only accepts positive values') } } From f2aad7bc57d2edad7d42a7d9d148558847fb31a6 Mon Sep 17 00:00:00 2001 From: leeyan44 <86589062+leeyan44@users.noreply.github.com> Date: Tue, 20 Jul 2021 12:38:47 +0800 Subject: [PATCH 5/5] Update on Volume.js formatting error --- Maths/Volume.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Maths/Volume.js b/Maths/Volume.js index b8a4f69f9..a539cda05 100644 --- a/Maths/Volume.js +++ b/Maths/Volume.js @@ -1,16 +1,16 @@ /* - Calculate the volume of the shapes - - Volume for Cuboid - Volume for Cube - Volume for Cone - Volume for Pyramid - Volume for Cylinder - Volume for Triangular Prism - Volume for Pentagonal Prism - Volume for Sphere - Volume for Hemisphere - */ +Calculate the volume of the shapes + +Volume for Cuboid +Volume for Cube +Volume for Cone +Volume for Pyramid +Volume for Cylinder +Volume for Triangular Prism +Volume for Pentagonal Prism +Volume for Sphere +Volume for Hemisphere +*/ /* Calculate the volume for a Cuboid @@ -42,7 +42,7 @@ const volCube = (length) => { const volCone = (radius, height) => { isNumber(radius, 'Radius') isNumber(height, 'Height') - return (Math.PI * radius ** 2 * height/3.0) + return (Math.PI * radius ** 2 * height / 3.0) } /* @@ -54,7 +54,7 @@ const volPyramid = (baseLength, baseWidth, height) => { isNumber(baseLength, 'BaseLength') isNumber(baseWidth, 'BaseWidth') isNumber(height, 'Height') - return (baseLength * baseWidth * height) / 3.0 + return (baseLength * baseWidth * height) / 3.0 } /* @@ -71,7 +71,7 @@ const volCylinder = (radius, height) => { /* Calculate the volume for a Triangular Prism Reference: http://lrd.kangan.edu.au/numbers/content/03_volume/04_page.htm - return 1 / 2 * baseLengthTriangle * heightTriangle * height + return 1 / 2 * baseLengthTriangle * heightTriangle * height */ const volTriangularPrism = (baseLengthTriangle, heightTriangle, height) => { isNumber(baseLengthTriangle, 'BaseLengthTriangle') @@ -89,7 +89,7 @@ const volPentagonalPrism = (pentagonalLength, pentagonalBaseLength, height) => { isNumber(pentagonalLength, 'PentagonalLength') isNumber(pentagonalBaseLength, 'PentagonalBaseLength') isNumber(height, 'Height') - return (5/2 * pentagonalLength * pentagonalBaseLength * height) + return (5 / 2 * pentagonalLength * pentagonalBaseLength * height) } /* @@ -99,7 +99,7 @@ const volPentagonalPrism = (pentagonalLength, pentagonalBaseLength, height) => { */ const volSphere = (radius) => { isNumber(radius, 'Radius') - return (4/3 * Math.PI * radius ** 3) + return (4 / 3 * Math.PI * radius ** 3) } /* @@ -109,7 +109,7 @@ const volSphere = (radius) => { */ const volHemisphere = (radius) => { isNumber(radius, 'Radius') - return (2.0 * Math.PI * radius ** 3)/ 3.0 + return (2.0 * Math.PI * radius ** 3) / 3.0 } const isNumber = (number, noName = 'number') => { @@ -120,4 +120,4 @@ const isNumber = (number, noName = 'number') => { } } -export {volCuboid, volCube, volCone, volPyramid, volCylinder, volTriangularPrism, volPentagonalPrism, volSphere, volHemisphere } +export { volCuboid, volCube, volCone, volPyramid, volCylinder, volTriangularPrism, volPentagonalPrism, volSphere, volHemisphere }