forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Area.js
184 lines (171 loc) · 5.44 KB
/
Area.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Calculate the area of various shapes
*/
/**
* @function surfaceAreaCube
* @description Calculate the Surface Area of a Cube.
* @param {Integer} side - Integer
* @return {Integer} - 6 * side ** 2
* @see [surfaceAreaCube](https://en.wikipedia.org/wiki/Area#Surface_area)
* @example surfaceAreaCube(1) = 6
*/
const surfaceAreaCube = (side) => {
validateNumericParam(side, 'side')
return 6 * side ** 2
}
/**
* @function surfaceAreaSphere
* @description Calculate the Surface Area of a Sphere.
* @param {Integer} radius - Integer
* @return {Integer} - 4 * pi * r^2
* @see [surfaceAreaSphere](https://en.wikipedia.org/wiki/Sphere)
* @example surfaceAreaSphere(5) = 314.1592653589793
*/
const surfaceAreaSphere = (radius) => {
validateNumericParam(radius, 'radius')
return 4.0 * Math.PI * radius ** 2.0
}
/**
* @function areaRectangle
* @description Calculate the area of a rectangle.
* @param {Integer} length - Integer
* @param {Integer} width - Integer
* @return {Integer} - width * length
* @see [areaRectangle](https://en.wikipedia.org/wiki/Area#Quadrilateral_area)
* @example areaRectangle(4) = 16
*/
const areaRectangle = (length, width) => {
validateNumericParam(length, 'Length')
validateNumericParam(width, 'Width')
return width * length
}
/**
* @function areaSquare
* @description Calculate the area of a square.
* @param {Integer} side - Integer
* @return {Integer} - side ** 2.
* @see [areaSquare](https://en.wikipedia.org/wiki/Square)
* @example areaSquare(4) = 16
*/
const areaSquare = (side) => {
validateNumericParam(side, 'square side')
return side ** 2
}
/**
* @function areaTriangle
* @description Calculate the area of a triangle.
* @param {Integer} base - Integer
* @param {Integer} height - Integer
* @return {Integer} - base * height / 2.
* @see [areaTriangle](https://en.wikipedia.org/wiki/Area#Triangle_area)
* @example areaTriangle(1.66, 3.44) = 2.8552
*/
const areaTriangle = (base, height) => {
validateNumericParam(base, 'Base')
validateNumericParam(height, 'Height')
return (base * height) / 2.0
}
/**
* @function areaTriangleWithAllThreeSides
* @description Calculate the area of a triangle with the all three sides given.
* @param {Integer} side1 - Integer
* @param {Integer} side2 - Integer
* @param {Integer} side3 - Integer
* @return {Integer} - area of triangle.
* @see [areaTriangleWithAllThreeSides](https://en.wikipedia.org/wiki/Heron%27s_formula)
* @example areaTriangleWithAllThreeSides(5, 6, 7) = 14.7
*/
const areaTriangleWithAllThreeSides = (side1, side2, side3) => {
validateNumericParam(side1, 'side1')
validateNumericParam(side2, 'side2')
validateNumericParam(side3, 'side3')
if (
side1 + side2 <= side3 ||
side1 + side3 <= side2 ||
side2 + side3 <= side1
) {
throw new TypeError('Invalid Triangle sides.')
}
// Finding Semi perimeter of the triangle using formula
const semi = (side1 + side2 + side3) / 2
// Calculating the area of the triangle
const area = Math.sqrt(
semi * (semi - side1) * (semi - side2) * (semi - side3)
)
return Number(area.toFixed(2))
}
/**
* @function areaParallelogram
* @description Calculate the area of a parallelogram.
* @param {Integer} base - Integer
* @param {Integer} height - Integer
* @return {Integer} - base * height
* @see [areaParallelogram](https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles)
* @example areaParallelogram(5, 6) = 24
*/
const areaParallelogram = (base, height) => {
validateNumericParam(base, 'Base')
validateNumericParam(height, 'Height')
return base * height
}
/**
* @function areaTrapezium
* @description Calculate the area of a trapezium.
* @param {Integer} base1 - Integer
* @param {Integer} base2 - Integer
* @param {Integer} height - Integer
* @return {Integer} - (1 / 2) * (base1 + base2) * height
* @see [areaTrapezium](https://en.wikipedia.org/wiki/Trapezoid)
* @example areaTrapezium(5, 12, 10) = 85
*/
const areaTrapezium = (base1, base2, height) => {
validateNumericParam(base1, 'Base One')
validateNumericParam(base2, 'Base Two')
validateNumericParam(height, 'Height')
return (1 / 2) * (base1 + base2) * height
}
/**
* @function areaCircle
* @description Calculate the area of a circle.
* @param {Integer} radius - Integer
* @return {Integer} - Math.PI * radius ** 2
* @see [areaCircle](https://en.wikipedia.org/wiki/Area_of_a_circle)
* @example areaCircle(5, 12, 10) = 85
*/
const areaCircle = (radius) => {
validateNumericParam(radius, 'Radius')
return Math.PI * radius ** 2
}
/**
* @function areaRhombus
* @description Calculate the area of a rhombus.
* @param {Integer} diagonal1 - Integer
* @param {Integer} diagonal2 - Integer
* @return {Integer} - (1 / 2) * diagonal1 * diagonal2
* @see [areaRhombus](https://en.wikipedia.org/wiki/Rhombus)
* @example areaRhombus(12, 10) = 60
*/
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,
areaTriangleWithAllThreeSides
}