forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolume.js
More file actions
Latest commit
133 lines (121 loc) · 3.5 KB
/
Copy pathVolume.js
File metadata and controls
133 lines (121 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
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
Reference: https://www.cuemath.com/measurement/volume-of-cuboid/
return width * length * height
*/
constvolCuboid=(width,length,height)=>{
isNumber(width,'Width')
isNumber(length,'Length')
isNumber(height,'Height')
returnwidth*length*height
}
/*
Calculate the volume for a Cube
Reference: https://www.cuemath.com/measurement/volume-of-cube/
return length * length * length
*/
constvolCube=(length)=>{
isNumber(length,'Length')
returnlength**3
}
/*
Calculate the volume for a Cone
Reference: https://www.cuemath.com/measurement/volume-of-cone/
return PI * radius^2 * height/3
*/
constvolCone=(radius,height)=>{
isNumber(radius,'Radius')
isNumber(height,'Height')
return(Math.PI*radius**2*height)/3.0
}
/*
Calculate the volume for a Pyramid
Reference: https://www.cuemath.com/measurement/volume-of-pyramid/
return (baseLength * baseWidth * height) / 3
*/
constvolPyramid=(baseLength,baseWidth,height)=>{
isNumber(baseLength,'BaseLength')
isNumber(baseWidth,'BaseWidth')
isNumber(height,'Height')
return(baseLength*baseWidth*height)/3.0
}
/*
Calculate the volume for a Cylinder
Reference: https://www.cuemath.com/measurement/volume-of-cylinder/
return PI * radius^2 * height
*/
constvolCylinder=(radius,height)=>{
isNumber(radius,'Radius')
isNumber(height,'Height')
returnMath.PI*radius**2*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
*/
constvolTriangularPrism=(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
Reference: https://www.cuemath.com/measurement/volume-of-pentagonal-prism/
return 5/2 * pentagonalLength * pentagonalBaseLength * height
*/
constvolPentagonalPrism=(pentagonalLength,pentagonalBaseLength,height)=>{
isNumber(pentagonalLength,'PentagonalLength')
isNumber(pentagonalBaseLength,'PentagonalBaseLength')
isNumber(height,'Height')
return(5/2)*pentagonalLength*pentagonalBaseLength*height
}
/*
Calculate the volume for a Sphere
Reference: https://www.cuemath.com/measurement/volume-of-sphere/
return 4/3 * PI * radius^3
*/
constvolSphere=(radius)=>{
isNumber(radius,'Radius')
return(4/3)*Math.PI*radius**3
}
/*
Calculate the volume for a Hemisphere
Reference: https://www.cuemath.com/measurement/volume-of-hemisphere/
return (2 * PI * radius^3)/3
*/
constvolHemisphere=(radius)=>{
isNumber(radius,'Radius')
return(2.0*Math.PI*radius**3)/3.0
}
constisNumber=(number,noName='number')=>{
if(typeofnumber!=='number'){
thrownewTypeError('The '+noName+' should be Number type')
}elseif(number<0||!Number.isFinite(number)){
thrownewError('The '+noName+' only accepts positive values')
}
}
export{
volCuboid,
volCube,
volCone,
volPyramid,
volCylinder,
volTriangularPrism,
volPentagonalPrism,
volSphere,
volHemisphere
}