Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e0cf98

Browse files
authored
fix: cleanup CoPrimeCheck (TheAlgorithms#1609)
1 parent fb0a99c commit 0e0cf98

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

‎Maths/CoPrimeCheck.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ const GetEuclidGCD = (arg1, arg2) => {
2828
const CoPrimeCheck = (firstNumber, secondNumber) => {
2929
// firstly, check that input is a number or not.
3030
if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') {
31-
return new TypeError('Argument is not a number.')
31+
throw new TypeError('Argument is not a number.')
3232
}
3333
/*
3434
This is the most efficient algorithm for checking co-primes
3535
if the GCD of both the numbers is 1 that means they are co-primes.
3636
*/
37-
return GetEuclidGCD(firstNumber,secondNumber) === 1
37+
return GetEuclidGCD(Math.abs(firstNumber),Math.abs(secondNumber)) === 1
3838
}
3939

4040
export { CoPrimeCheck }

‎Maths/test/CoPrimeCheck.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { CoPrimeCheck } from '../CoPrimeCheck'
2+
3+
describe('CoPrimeCheck', () => {
4+
it.each([
5+
[1, 1],
6+
[1, 2],
7+
[1, 3],
8+
[1, 7],
9+
[20, 21],
10+
[5, 7],
11+
[-5, -7]
12+
])('returns true for %j and %i', (inputA, inputB) => {
13+
expect(CoPrimeCheck(inputA, inputB)).toBe(true)
14+
expect(CoPrimeCheck(inputB, inputA)).toBe(true)
15+
})
16+
17+
it.each([
18+
[5, 15],
19+
[13 * 17 * 19, 17 * 23 * 29]
20+
])('returns false for %j and %i', (inputA, inputB) => {
21+
expect(CoPrimeCheck(inputA, inputB)).toBe(false)
22+
expect(CoPrimeCheck(inputB, inputA)).toBe(false)
23+
})
24+
25+
it('should throw when any of the inputs is not a number', () => {
26+
expect(() => CoPrimeCheck('1', 2)).toThrowError()
27+
expect(() => CoPrimeCheck(1, '2')).toThrowError()
28+
})
29+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /