Skip to content

Commit

Permalink
merge: add areaTriangleWithAllThreeSides method in Math/Area.js (TheA…
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatin-kathuria authored Dec 15, 2021
1 parent 3d2a48f commit ef832f6
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 48 deletions.
181 changes: 133 additions & 48 deletions Maths/Area.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,165 @@
/*
Calculate the area of various shapes
*/

Calculate the Surface Area of a Cube.
Example: surfaceAreaCube(1) will return 6
More about: https://en.wikipedia.org/wiki/Area#Surface_area
/**
* @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 = (sideLength) => {
validateNumericParam(sideLength, 'sideLength')
return (6.0 * sideLength ** 2.0)
const surfaceAreaCube = (side) => {
validateNumericParam(side, 'side')
return 6 * side ** 2
}

/*
Calculate the Surface Area of a Sphere.
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
return 4 * pi * r^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)
return 4.0 * Math.PI * radius ** 2.0
}

/*
Calculate the area of a rectangle
Wikipedia reference: https://en.wikipedia.org/wiki/Area#Quadrilateral_area
return width * length
*/
/**
* @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)
return width * length
}

/*
Calculate the area of a square
*/
const areaSquare = (sideLength) => {
validateNumericParam(sideLength, 'side length')
return (sideLength ** 2)
/**
* @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
}

/*
Calculate the area of a triangle
Wikipedia reference: https://en.wikipedia.org/wiki/Area#Triangle_area
return base * height / 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
}

/*
Calculate the area of a parallelogram
Wikipedia reference: https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles
*/
/**
* @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)
return base * height
}

/*
Calculate the area of a trapezium
*/
/**
* @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.0 / 2.0 * (base1 + base2) * height
return (1 / 2) * (base1 + base2) * height
}

/*
Calculate the area of a circle
*/
/**
* @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)
return Math.PI * radius ** 2
}

/*
Calculate the area of a rhombus
Wikipedia reference: https://en.wikipedia.org/wiki/Rhombus
*/
/**
* @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)
return (1 / 2) * diagonal1 * diagonal2
}

const validateNumericParam = (param, paramName = 'param') => {
Expand All @@ -96,4 +170,15 @@ const validateNumericParam = (param, paramName = 'param') => {
}
}

export { surfaceAreaCube, surfaceAreaSphere, areaRectangle, areaSquare, areaTriangle, areaParallelogram, areaTrapezium, areaCircle, areaRhombus }
export {
surfaceAreaCube,
surfaceAreaSphere,
areaRectangle,
areaSquare,
areaTriangle,
areaParallelogram,
areaTrapezium,
areaCircle,
areaRhombus,
areaTriangleWithAllThreeSides
}
20 changes: 20 additions & 0 deletions Maths/test/Area.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Testing surfaceAreaCube calculations', () => {
expect(() => area.surfaceAreaCube('199')).toThrow()
})
})

describe('Testing surfaceAreaSphere calculations', () => {
it('with correct value', () => {
const calculateArea = area.surfaceAreaSphere(5)
Expand All @@ -24,6 +25,7 @@ describe('Testing surfaceAreaSphere calculations', () => {
expect(() => area.surfaceAreaSphere(-1)).toThrow()
})
})

describe('Testing areaRectangle calculations', () => {
it('with correct args', () => {
const areaRectangle = area.areaRectangle(2.5, 2)
Expand All @@ -36,6 +38,7 @@ describe('Testing areaRectangle calculations', () => {
expect(() => area.areaRectangle(23, 'zero')).toThrow()
})
})

describe('Testing areaSquare calculations', () => {
it('with correct args', () => {
const areaSquare = area.areaSquare(2.5)
Expand All @@ -46,6 +49,7 @@ describe('Testing areaSquare calculations', () => {
expect(() => area.areaSquare('zero')).toThrow()
})
})

describe('Testing areaTriangle calculations', () => {
it('with correct args', () => {
const areaTriangle = area.areaTriangle(1.66, 3.44)
Expand All @@ -56,6 +60,19 @@ describe('Testing areaTriangle calculations', () => {
expect(() => area.areaTriangle(9, 'zero')).toThrow()
})
})

describe('Testing areaTriangleWithAllThreeSides calculations', () => {
it('with correct args', () => {
const areaTriangle = area.areaTriangleWithAllThreeSides(5, 6, 7)
expect(areaTriangle).toBe(14.7)
})
it('with incorrect sides, expect throw', () => {
expect(() => area.areaTriangleWithAllThreeSides(-1, 1, 10)).toThrow()
expect(() => area.areaTriangleWithAllThreeSides(9, 'zero', 2)).toThrow()
expect(() => area.areaTriangleWithAllThreeSides(1, 10, 12)).toThrow()
})
})

describe('Testing areaParallelogram calculations', () => {
it('with correct args', () => {
const areaParallelogram = area.areaParallelogram(1.66, 3.44)
Expand All @@ -66,6 +83,7 @@ describe('Testing areaParallelogram calculations', () => {
expect(() => area.areaParallelogram(9, 'zero')).toThrow()
})
})

describe('Testing areaTrapezium calculations', () => {
it('with correct args', () => {
const areaTrapezium = area.areaTrapezium(1.66, 2.41, 4.1)
Expand All @@ -77,6 +95,7 @@ describe('Testing areaTrapezium calculations', () => {
expect(() => area.areaTrapezium(9, 1, 'seven')).toThrow()
})
})

describe('Testing areaCircle calculations', () => {
it('with correct args', () => {
const areaCircle = area.areaCircle(3.456)
Expand All @@ -87,6 +106,7 @@ describe('Testing areaCircle calculations', () => {
expect(() => area.areaCircle('zero')).toThrow()
})
})

describe('Testing areaRhombus calculations', () => {
it('with correct args', () => {
const areaRhombus = area.areaRhombus(2.5, 2.0)
Expand Down

0 comments on commit ef832f6

Please sign in to comment.