Skip to content

Commit 0ad515a

Browse files
Added new area calculators (#428)
1 parent 731f885 commit 0ad515a

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

Maths/Area.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Calculate the area of various shapes
3+
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
7+
*/
8+
const surfaceAreaCube = (sideLength) => {
9+
validateNumericParam(sideLength, 'sideLength')
10+
return (6.0 * sideLength ** 2.0)
11+
}
12+
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+
const surfaceAreaSphere = (radius) => {
19+
validateNumericParam(radius, 'radius')
20+
return (4.0 * Math.PI * radius ** 2.0)
21+
}
22+
23+
/*
24+
Calculate the area of a rectangle
25+
Wikipedia reference: https://en.wikipedia.org/wiki/Area#Quadrilateral_area
26+
return width * length
27+
*/
28+
const areaRectangle = (length, width) => {
29+
validateNumericParam(length, 'Length')
30+
validateNumericParam(width, 'Width')
31+
return (width * length)
32+
}
33+
34+
/*
35+
Calculate the area of a square
36+
*/
37+
const areaSquare = (sideLength) => {
38+
validateNumericParam(sideLength, 'side length')
39+
return (sideLength ** 2)
40+
}
41+
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+
*/
47+
const areaTriangle = (base, height) => {
48+
validateNumericParam(base, 'Base')
49+
validateNumericParam(height, 'Height')
50+
return (base * height) / 2.0
51+
}
52+
53+
/*
54+
Calculate the area of a parallelogram
55+
Wikipedia reference: https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles
56+
*/
57+
const areaParallelogram = (base, height) => {
58+
validateNumericParam(base, 'Base')
59+
validateNumericParam(height, 'Height')
60+
return (base * height)
61+
}
62+
63+
/*
64+
Calculate the area of a trapezium
65+
*/
66+
const areaTrapezium = (base1, base2, height) => {
67+
validateNumericParam(base1, 'Base One')
68+
validateNumericParam(base2, 'Base Two')
69+
validateNumericParam(height, 'Height')
70+
return 1.0 / 2.0 * (base1 + base2) * height
71+
}
72+
73+
/*
74+
Calculate the area of a circle
75+
*/
76+
const areaCircle = (radius) => {
77+
validateNumericParam(radius, 'Radius')
78+
return (Math.PI * radius ** 2)
79+
}
80+
81+
/*
82+
Calculate the area of a rhombus
83+
Wikipedia reference: https://en.wikipedia.org/wiki/Rhombus
84+
*/
85+
const areaRhombus = (diagonal1, diagonal2) => {
86+
validateNumericParam(diagonal1, 'diagonal one')
87+
validateNumericParam(diagonal2, 'diagonal two')
88+
return (1 / 2 * diagonal1 * diagonal2)
89+
}
90+
91+
const validateNumericParam = (param, paramName = 'param') => {
92+
if (typeof param !== 'number') {
93+
throw new TypeError('The ' + paramName + ' should be type Number')
94+
} else if (param < 0) {
95+
throw new Error('The ' + paramName + ' only accepts non-negative values')
96+
}
97+
}
98+
99+
export { surfaceAreaCube, surfaceAreaSphere, areaRectangle, areaSquare, areaTriangle, areaParallelogram, areaTrapezium, areaCircle, areaRhombus }

Maths/Area.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import * as area from './Area'
2+
3+
describe('Testing surfaceAreaCube calculations', () => {
4+
it('with natural number', () => {
5+
const surfaceAreaOfOne = area.surfaceAreaCube(1.2)
6+
const surfaceAreaOfThree = area.surfaceAreaCube(3)
7+
expect(surfaceAreaOfOne).toBe(8.64)
8+
expect(surfaceAreaOfThree).toBe(54)
9+
})
10+
it('with negative argument, expect throw', () => {
11+
expect(() => area.surfaceAreaCube(-1)).toThrow()
12+
})
13+
it('with non-numeric argument, expect throw', () => {
14+
expect(() => area.surfaceAreaCube('199')).toThrow()
15+
})
16+
})
17+
describe('Testing surfaceAreaSphere calculations', () => {
18+
it('with correct value', () => {
19+
const calculateArea = area.surfaceAreaSphere(5)
20+
const expected = 314.1592653589793
21+
expect(calculateArea).toBe(expected)
22+
})
23+
it('with negative value, expect throw', () => {
24+
expect(() => area.surfaceAreaSphere(-1)).toThrow()
25+
})
26+
})
27+
describe('Testing areaRectangle calculations', () => {
28+
it('with correct args', () => {
29+
const areaRectangle = area.areaRectangle(2.5, 2)
30+
expect(areaRectangle).toBe(5.0)
31+
})
32+
it('with incorrect args, expect throw', () => {
33+
expect(() => area.areaRectangle(-1, 20)).toThrow()
34+
expect(() => area.areaRectangle('1', 0)).toThrow()
35+
expect(() => area.areaRectangle(23, -1)).toThrow()
36+
expect(() => area.areaRectangle(23, 'zero')).toThrow()
37+
})
38+
})
39+
describe('Testing areaSquare calculations', () => {
40+
it('with correct args', () => {
41+
const areaSquare = area.areaSquare(2.5)
42+
expect(areaSquare).toBe(6.25)
43+
})
44+
it('with incorrect side length, expect throw', () => {
45+
expect(() => area.areaSquare(-1)).toThrow()
46+
expect(() => area.areaSquare('zero')).toThrow()
47+
})
48+
})
49+
describe('Testing areaTriangle calculations', () => {
50+
it('with correct args', () => {
51+
const areaTriangle = area.areaTriangle(1.66, 3.44)
52+
expect(areaTriangle).toBe(2.8552)
53+
})
54+
it('with incorrect base and height, expect throw', () => {
55+
expect(() => area.areaTriangle(-1, 1)).toThrow()
56+
expect(() => area.areaTriangle(9, 'zero')).toThrow()
57+
})
58+
})
59+
describe('Testing areaParallelogram calculations', () => {
60+
it('with correct args', () => {
61+
const areaParallelogram = area.areaParallelogram(1.66, 3.44)
62+
expect(areaParallelogram).toBe(5.7104)
63+
})
64+
it('with incorrect base and height, expect throw', () => {
65+
expect(() => area.areaParallelogram(-1, 1)).toThrow()
66+
expect(() => area.areaParallelogram(9, 'zero')).toThrow()
67+
})
68+
})
69+
describe('Testing areaTrapezium calculations', () => {
70+
it('with correct args', () => {
71+
const areaTrapezium = area.areaTrapezium(1.66, 2.41, 4.1)
72+
expect(areaTrapezium).toBe(8.3435)
73+
})
74+
it('with incorrect bases and height, expect throw', () => {
75+
expect(() => area.areaTrapezium(-1, 1, 0)).toThrow()
76+
expect(() => area.areaTrapezium(9, 'zero', 2)).toThrow()
77+
expect(() => area.areaTrapezium(9, 1, 'seven')).toThrow()
78+
})
79+
})
80+
describe('Testing areaCircle calculations', () => {
81+
it('with correct args', () => {
82+
const areaCircle = area.areaCircle(3.456)
83+
expect(areaCircle).toBe(37.52298159254666)
84+
})
85+
it('with incorrect diagonal, expect throw', () => {
86+
expect(() => area.areaCircle(-1)).toThrow()
87+
expect(() => area.areaCircle('zero')).toThrow()
88+
})
89+
})
90+
describe('Testing areaRhombus calculations', () => {
91+
it('with correct args', () => {
92+
const areaRhombus = area.areaRhombus(2.5, 2.0)
93+
expect(areaRhombus).toBe(2.5)
94+
})
95+
it('with incorrect diagonals, expect throw', () => {
96+
expect(() => area.areaRhombus(7, -1)).toThrow()
97+
expect(() => area.areaRhombus('zero', 2)).toThrow()
98+
})
99+
})

0 commit comments

Comments
 (0)