-
-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathhexagon_area.ts
More file actions
28 lines (21 loc) · 779 Bytes
/
hexagon_area.ts
File metadata and controls
28 lines (21 loc) · 779 Bytes
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
/**
* @function hexArea
* @description Returns area of a regular hexagon
* @summary Finds the area of a regular hexagon (all 6 sides are equal lenght)
* @param {Number} num - A natural number
* @return {number} The area of a regular hexagon
* @see [Wikipedia](https://en.wikipedia.org/wiki/Hexagon)
* @example hexArea(3) = 23.382685902179844
* @example hexArea(10) = 259.8076211353316
*/
export const hexArea = (side: number): number => {
if (side <= 0) {
throw new TypeError('Invalid Input')
}
let area = ((3 * (3 ** (1/2)) / 2)) * (side ** 2)
// Round to nearest integer if close enough due to imprecise float
if (Math.abs(area - Math.round(area)) < 0.000000000000001) {
area = Math.round(area);
}
return area
}