|
| 1 | +/* |
| 2 | +Calculate the volume of the shapes |
| 3 | +
|
| 4 | +Volume for Cuboid |
| 5 | +Volume for Cube |
| 6 | +Volume for Cone |
| 7 | +Volume for Pyramid |
| 8 | +Volume for Cylinder |
| 9 | +Volume for Triangular Prism |
| 10 | +Volume for Pentagonal Prism |
| 11 | +Volume for Sphere |
| 12 | +Volume for Hemisphere |
| 13 | +*/ |
| 14 | + |
| 15 | +/* |
| 16 | + Calculate the volume for a Cuboid |
| 17 | + Reference: https://www.cuemath.com/measurement/volume-of-cuboid/ |
| 18 | + return width * length * height |
| 19 | +*/ |
| 20 | +const volCuboid = (width, length, height) => { |
| 21 | + isNumber(width, 'Width') |
| 22 | + isNumber(length, 'Length') |
| 23 | + isNumber(height, 'Height') |
| 24 | + return (width * length * height) |
| 25 | +} |
| 26 | + |
| 27 | +/* |
| 28 | + Calculate the volume for a Cube |
| 29 | + Reference: https://www.cuemath.com/measurement/volume-of-cube/ |
| 30 | + return length * length * length |
| 31 | +*/ |
| 32 | +const volCube = (length) => { |
| 33 | + isNumber(length, 'Length') |
| 34 | + return (length ** 3) |
| 35 | +} |
| 36 | + |
| 37 | +/* |
| 38 | + Calculate the volume for a Cone |
| 39 | + Reference: https://www.cuemath.com/measurement/volume-of-cone/ |
| 40 | + return PI * radius^2 * height/3 |
| 41 | +*/ |
| 42 | +const volCone = (radius, height) => { |
| 43 | + isNumber(radius, 'Radius') |
| 44 | + isNumber(height, 'Height') |
| 45 | + return (Math.PI * radius ** 2 * height / 3.0) |
| 46 | +} |
| 47 | + |
| 48 | +/* |
| 49 | + Calculate the volume for a Pyramid |
| 50 | + Reference: https://www.cuemath.com/measurement/volume-of-pyramid/ |
| 51 | + return (baseLength * baseWidth * height) / 3 |
| 52 | +*/ |
| 53 | +const volPyramid = (baseLength, baseWidth, height) => { |
| 54 | + isNumber(baseLength, 'BaseLength') |
| 55 | + isNumber(baseWidth, 'BaseWidth') |
| 56 | + isNumber(height, 'Height') |
| 57 | + return (baseLength * baseWidth * height) / 3.0 |
| 58 | +} |
| 59 | + |
| 60 | +/* |
| 61 | + Calculate the volume for a Cylinder |
| 62 | + Reference: https://www.cuemath.com/measurement/volume-of-cylinder/ |
| 63 | + return PI * radius^2 * height |
| 64 | +*/ |
| 65 | +const volCylinder = (radius, height) => { |
| 66 | + isNumber(radius, 'Radius') |
| 67 | + isNumber(height, 'Height') |
| 68 | + return (Math.PI * radius ** 2 * height) |
| 69 | +} |
| 70 | + |
| 71 | +/* |
| 72 | + Calculate the volume for a Triangular Prism |
| 73 | + Reference: http://lrd.kangan.edu.au/numbers/content/03_volume/04_page.htm |
| 74 | + return 1 / 2 * baseLengthTriangle * heightTriangle * height |
| 75 | +*/ |
| 76 | +const volTriangularPrism = (baseLengthTriangle, heightTriangle, height) => { |
| 77 | + isNumber(baseLengthTriangle, 'BaseLengthTriangle') |
| 78 | + isNumber(heightTriangle, 'HeightTriangle') |
| 79 | + isNumber(height, 'Height') |
| 80 | + return (1 / 2 * baseLengthTriangle * heightTriangle * height) |
| 81 | +} |
| 82 | + |
| 83 | +/* |
| 84 | + Calculate the volume for a Pentagonal Prism |
| 85 | + Reference: https://www.cuemath.com/measurement/volume-of-pentagonal-prism/ |
| 86 | + return 5/2 * pentagonalLength * pentagonalBaseLength * height |
| 87 | +*/ |
| 88 | +const volPentagonalPrism = (pentagonalLength, pentagonalBaseLength, height) => { |
| 89 | + isNumber(pentagonalLength, 'PentagonalLength') |
| 90 | + isNumber(pentagonalBaseLength, 'PentagonalBaseLength') |
| 91 | + isNumber(height, 'Height') |
| 92 | + return (5 / 2 * pentagonalLength * pentagonalBaseLength * height) |
| 93 | +} |
| 94 | + |
| 95 | +/* |
| 96 | + Calculate the volume for a Sphere |
| 97 | + Reference: https://www.cuemath.com/measurement/volume-of-sphere/ |
| 98 | + return 4/3 * PI * radius^3 |
| 99 | +*/ |
| 100 | +const volSphere = (radius) => { |
| 101 | + isNumber(radius, 'Radius') |
| 102 | + return (4 / 3 * Math.PI * radius ** 3) |
| 103 | +} |
| 104 | + |
| 105 | +/* |
| 106 | + Calculate the volume for a Hemisphere |
| 107 | + Reference: https://www.cuemath.com/measurement/volume-of-hemisphere/ |
| 108 | + return (2 * PI * radius^3)/3 |
| 109 | +*/ |
| 110 | +const volHemisphere = (radius) => { |
| 111 | + isNumber(radius, 'Radius') |
| 112 | + return (2.0 * Math.PI * radius ** 3) / 3.0 |
| 113 | +} |
| 114 | + |
| 115 | +const isNumber = (number, noName = 'number') => { |
| 116 | + if (typeof number !== 'number') { |
| 117 | + throw new TypeError('The ' + noName + ' should be Number type') |
| 118 | + } else if (number < 0 || (!Number.isFinite(number))) { |
| 119 | + throw new Error('The ' + noName + ' only accepts positive values') |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export { volCuboid, volCube, volCone, volPyramid, volCylinder, volTriangularPrism, volPentagonalPrism, volSphere, volHemisphere } |
0 commit comments