1
1
/*
2
2
Calculate the area of various shapes
3
+ */
3
4
4
- Calculate the Surface Area of a Cube.
5
- Example: surfaceAreaCube(1) will return 6
6
- More about: https://en.wikipedia.org/wiki/Area#Surface_area
5
+ /**
6
+ * @function surfaceAreaCube
7
+ * @description Calculate the Surface Area of a Cube.
8
+ * @param {Integer } side - Integer
9
+ * @return {Integer } - 6 * side ** 2
10
+ * @see [surfaceAreaCube](https://en.wikipedia.org/wiki/Area#Surface_area)
11
+ * @example surfaceAreaCube(1) = 6
7
12
*/
8
- const surfaceAreaCube = ( sideLength ) => {
9
- validateNumericParam ( sideLength , 'sideLength ' )
10
- return ( 6.0 * sideLength ** 2.0 )
13
+ const surfaceAreaCube = ( side ) => {
14
+ validateNumericParam ( side , 'side ' )
15
+ return 6 * side ** 2
11
16
}
12
17
13
- /*
14
- Calculate the Surface Area of a Sphere.
15
- Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
16
- return 4 * pi * r^2
17
- */
18
+ /**
19
+ * @function surfaceAreaSphere
20
+ * @description Calculate the Surface Area of a Sphere.
21
+ * @param {Integer } radius - Integer
22
+ * @return {Integer } - 4 * pi * r^2
23
+ * @see [surfaceAreaSphere](https://en.wikipedia.org/wiki/Sphere)
24
+ * @example surfaceAreaSphere(5) = 314.1592653589793
25
+ */
18
26
const surfaceAreaSphere = ( radius ) => {
19
27
validateNumericParam ( radius , 'radius' )
20
- return ( 4.0 * Math . PI * radius ** 2.0 )
28
+ return 4.0 * Math . PI * radius ** 2.0
21
29
}
22
30
23
- /*
24
- Calculate the area of a rectangle
25
- Wikipedia reference: https://en.wikipedia.org/wiki/Area#Quadrilateral_area
26
- return width * length
27
- */
31
+ /**
32
+ * @function areaRectangle
33
+ * @description Calculate the area of a rectangle.
34
+ * @param {Integer } length - Integer
35
+ * @param {Integer } width - Integer
36
+ * @return {Integer } - width * length
37
+ * @see [areaRectangle](https://en.wikipedia.org/wiki/Area#Quadrilateral_area)
38
+ * @example areaRectangle(4) = 16
39
+ */
28
40
const areaRectangle = ( length , width ) => {
29
41
validateNumericParam ( length , 'Length' )
30
42
validateNumericParam ( width , 'Width' )
31
- return ( width * length )
43
+ return width * length
32
44
}
33
45
34
- /*
35
- Calculate the area of a square
36
- */
37
- const areaSquare = ( sideLength ) => {
38
- validateNumericParam ( sideLength , 'side length' )
39
- return ( sideLength ** 2 )
46
+ /**
47
+ * @function areaSquare
48
+ * @description Calculate the area of a square.
49
+ * @param {Integer } side - Integer
50
+ * @return {Integer } - side ** 2.
51
+ * @see [areaSquare](https://en.wikipedia.org/wiki/Square)
52
+ * @example areaSquare(4) = 16
53
+ */
54
+ const areaSquare = ( side ) => {
55
+ validateNumericParam ( side , 'square side' )
56
+ return side ** 2
40
57
}
41
58
42
- /*
43
- Calculate the area of a triangle
44
- Wikipedia reference: https://en.wikipedia.org/wiki/Area#Triangle_area
45
- return base * height / 2
46
- */
59
+ /**
60
+ * @function areaTriangle
61
+ * @description Calculate the area of a triangle.
62
+ * @param {Integer } base - Integer
63
+ * @param {Integer } height - Integer
64
+ * @return {Integer } - base * height / 2.
65
+ * @see [areaTriangle](https://en.wikipedia.org/wiki/Area#Triangle_area)
66
+ * @example areaTriangle(1.66, 3.44) = 2.8552
67
+ */
47
68
const areaTriangle = ( base , height ) => {
48
69
validateNumericParam ( base , 'Base' )
49
70
validateNumericParam ( height , 'Height' )
50
71
return ( base * height ) / 2.0
51
72
}
52
73
53
- /*
54
- Calculate the area of a parallelogram
55
- Wikipedia reference: https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles
56
- */
74
+ /**
75
+ * @function areaTriangleWithAllThreeSides
76
+ * @description Calculate the area of a triangle with the all three sides given.
77
+ * @param {Integer } side1 - Integer
78
+ * @param {Integer } side2 - Integer
79
+ * @param {Integer } side3 - Integer
80
+ * @return {Integer } - area of triangle.
81
+ * @see [areaTriangleWithAllThreeSides](https://en.wikipedia.org/wiki/Heron%27s_formula)
82
+ * @example areaTriangleWithAllThreeSides(5, 6, 7) = 14.7
83
+ */
84
+ const areaTriangleWithAllThreeSides = ( side1 , side2 , side3 ) => {
85
+ validateNumericParam ( side1 , 'side1' )
86
+ validateNumericParam ( side2 , 'side2' )
87
+ validateNumericParam ( side3 , 'side3' )
88
+ if (
89
+ side1 + side2 <= side3 ||
90
+ side1 + side3 <= side2 ||
91
+ side2 + side3 <= side1
92
+ ) {
93
+ throw new TypeError ( 'Invalid Triangle sides.' )
94
+ }
95
+ // Finding Semi perimeter of the triangle using formula
96
+ const semi = ( side1 + side2 + side3 ) / 2
97
+
98
+ // Calculating the area of the triangle
99
+ const area = Math . sqrt (
100
+ semi * ( semi - side1 ) * ( semi - side2 ) * ( semi - side3 )
101
+ )
102
+ return Number ( area . toFixed ( 2 ) )
103
+ }
104
+
105
+ /**
106
+ * @function areaParallelogram
107
+ * @description Calculate the area of a parallelogram.
108
+ * @param {Integer } base - Integer
109
+ * @param {Integer } height - Integer
110
+ * @return {Integer } - base * height
111
+ * @see [areaParallelogram](https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles)
112
+ * @example areaParallelogram(5, 6) = 24
113
+ */
57
114
const areaParallelogram = ( base , height ) => {
58
115
validateNumericParam ( base , 'Base' )
59
116
validateNumericParam ( height , 'Height' )
60
- return ( base * height )
117
+ return base * height
61
118
}
62
119
63
- /*
64
- Calculate the area of a trapezium
65
- */
120
+ /**
121
+ * @function areaTrapezium
122
+ * @description Calculate the area of a trapezium.
123
+ * @param {Integer } base1 - Integer
124
+ * @param {Integer } base2 - Integer
125
+ * @param {Integer } height - Integer
126
+ * @return {Integer } - (1 / 2) * (base1 + base2) * height
127
+ * @see [areaTrapezium](https://en.wikipedia.org/wiki/Trapezoid)
128
+ * @example areaTrapezium(5, 12, 10) = 85
129
+ */
66
130
const areaTrapezium = ( base1 , base2 , height ) => {
67
131
validateNumericParam ( base1 , 'Base One' )
68
132
validateNumericParam ( base2 , 'Base Two' )
69
133
validateNumericParam ( height , 'Height' )
70
- return 1.0 / 2.0 * ( base1 + base2 ) * height
134
+ return ( 1 / 2 ) * ( base1 + base2 ) * height
71
135
}
72
136
73
- /*
74
- Calculate the area of a circle
75
- */
137
+ /**
138
+ * @function areaCircle
139
+ * @description Calculate the area of a circle.
140
+ * @param {Integer } radius - Integer
141
+ * @return {Integer } - Math.PI * radius ** 2
142
+ * @see [areaCircle](https://en.wikipedia.org/wiki/Area_of_a_circle)
143
+ * @example areaCircle(5, 12, 10) = 85
144
+ */
76
145
const areaCircle = ( radius ) => {
77
146
validateNumericParam ( radius , 'Radius' )
78
- return ( Math . PI * radius ** 2 )
147
+ return Math . PI * radius ** 2
79
148
}
80
149
81
- /*
82
- Calculate the area of a rhombus
83
- Wikipedia reference: https://en.wikipedia.org/wiki/Rhombus
84
- */
150
+ /**
151
+ * @function areaRhombus
152
+ * @description Calculate the area of a rhombus.
153
+ * @param {Integer } diagonal1 - Integer
154
+ * @param {Integer } diagonal2 - Integer
155
+ * @return {Integer } - (1 / 2) * diagonal1 * diagonal2
156
+ * @see [areaRhombus](https://en.wikipedia.org/wiki/Rhombus)
157
+ * @example areaRhombus(12, 10) = 60
158
+ */
85
159
const areaRhombus = ( diagonal1 , diagonal2 ) => {
86
160
validateNumericParam ( diagonal1 , 'diagonal one' )
87
161
validateNumericParam ( diagonal2 , 'diagonal two' )
88
- return ( 1 / 2 * diagonal1 * diagonal2 )
162
+ return ( 1 / 2 ) * diagonal1 * diagonal2
89
163
}
90
164
91
165
const validateNumericParam = ( param , paramName = 'param' ) => {
@@ -96,4 +170,15 @@ const validateNumericParam = (param, paramName = 'param') => {
96
170
}
97
171
}
98
172
99
- export { surfaceAreaCube , surfaceAreaSphere , areaRectangle , areaSquare , areaTriangle , areaParallelogram , areaTrapezium , areaCircle , areaRhombus }
173
+ export {
174
+ surfaceAreaCube ,
175
+ surfaceAreaSphere ,
176
+ areaRectangle ,
177
+ areaSquare ,
178
+ areaTriangle ,
179
+ areaParallelogram ,
180
+ areaTrapezium ,
181
+ areaCircle ,
182
+ areaRhombus ,
183
+ areaTriangleWithAllThreeSides
184
+ }
0 commit comments