Skip to content

Commit c067a34

Browse files
authored
fix: GetEuclidGCD(0, 0) is 0 (#1621)
1 parent 0e0cf98 commit c067a34

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

Diff for: Maths/GetEuclidGCD.js

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export function GetEuclidGCD(a, b) {
88
if (typeof a !== 'number' || typeof b !== 'number') {
99
throw new TypeError('Arguments must be numbers')
1010
}
11-
if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0
1211
a = Math.abs(a)
1312
b = Math.abs(b)
1413
while (b !== 0) {

Diff for: Maths/test/GetEuclidGCD.test.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import { GetEuclidGCD } from '../GetEuclidGCD'
22

3-
function testEuclidGCD(n, m, expected) {
4-
test('Testing on ' + n + ' and ' + m + '!', () => {
5-
expect(GetEuclidGCD(n, m)).toBe(expected)
3+
describe('GetEuclidGCD', () => {
4+
it.each([
5+
[5, 20, 5],
6+
[109, 902, 1],
7+
[290, 780, 10],
8+
[104, 156, 52],
9+
[0, 100, 100],
10+
[-5, 50, 5],
11+
[0, 0, 0],
12+
[1, 1234567, 1]
13+
])('returns correct result for %i and %j', (inputA, inputB, expected) => {
14+
expect(GetEuclidGCD(inputA, inputB)).toBe(expected)
15+
expect(GetEuclidGCD(inputB, inputA)).toBe(expected)
616
})
7-
}
817

9-
testEuclidGCD(5, 20, 5)
10-
testEuclidGCD(109, 902, 1)
11-
testEuclidGCD(290, 780, 10)
12-
testEuclidGCD(104, 156, 52)
18+
it('should throw when any of the inputs is not a number', () => {
19+
expect(() => GetEuclidGCD('1', 2)).toThrowError()
20+
expect(() => GetEuclidGCD(1, '2')).toThrowError()
21+
})
22+
})

0 commit comments

Comments
 (0)