Skip to content

Commit 41d2d21

Browse files
authored
Merge pull request #30 from cwh0430/PythagoreanTheorem
Added PythagoreanTheorem.js
2 parents a3c2fa3 + 01b55e1 commit 41d2d21

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Maths/PythagoreanTheorem.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @function calcHypotenuse
3+
* @description Calculate the hypothenuse of a triangle.
4+
* @param {Integer} base - Integer
5+
* @param {Integer} adjacent - Integer
6+
* @return {Integer} - hypotenuse
7+
* @see [calcHypotenuse](https://en.wikipedia.org/wiki/Pythagorean_theorem)
8+
* @example calcHypotenuse(6,8) = 10
9+
*/
10+
const calcHypotenuse = (base, adjacent) => {
11+
const hypotenuse = Math.sqrt(base ** 2 + adjacent ** 2)
12+
return hypotenuse
13+
}
14+
15+
/**
16+
* @function calcOtherSides
17+
* @description Calculate the other sides of a triangle.
18+
* @param {Integer} side1 - Integer
19+
* @param {Integer} side2 - Integer
20+
* @return {Integer} - sides
21+
* @see [calcOtherSides](https://en.wikipedia.org/wiki/Pythagorean_theorem)
22+
* @example calcOtherSides(6,10) = 8
23+
*/
24+
const calcOtherSides = (side1, side2) => {
25+
if (side1 > side2) {
26+
const side = Math.sqrt(side1 ** 2 - side2 ** 2)
27+
return side
28+
} else if (side2 > side1) {
29+
const side = Math.sqrt(side2 ** 2 - side1 ** 2)
30+
return side
31+
}
32+
33+
return 'Both sides cannot be the same value'
34+
}
35+
36+
export {
37+
calcHypotenuse,
38+
calcOtherSides
39+
}

Maths/test/PythagoreanTheorem.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as py from '../PythagoreanTheorem'
2+
3+
describe('Testing calcHypotenuse calculations', () => {
4+
it('with natural number', () => {
5+
const result = py.calcHypotenuse(6, 8)
6+
expect(result).toBe(10)
7+
})
8+
})
9+
10+
describe('Testing calcOtherSides calculations', () => {
11+
it('with side1 bigger than side2', () => {
12+
const result = py.calcOtherSides(6, 10)
13+
expect(result).toBe(8)
14+
})
15+
it('with side2 bigger than side1', () => {
16+
const result = py.calcOtherSides(10, 6)
17+
expect(result).toBe(8)
18+
})
19+
it('with side1 equals side2', () => {
20+
const result = py.calcOtherSides(10, 10)
21+
expect(result).toBe('Both sides cannot be the same value')
22+
})
23+
})

0 commit comments

Comments
 (0)