File tree 2 files changed +31
-2
lines changed
2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -28,13 +28,13 @@ const GetEuclidGCD = (arg1, arg2) => {
28
28
const CoPrimeCheck = ( firstNumber , secondNumber ) => {
29
29
// firstly, check that input is a number or not.
30
30
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.' )
32
32
}
33
33
/*
34
34
This is the most efficient algorithm for checking co-primes
35
35
if the GCD of both the numbers is 1 that means they are co-primes.
36
36
*/
37
- return GetEuclidGCD ( firstNumber , secondNumber ) === 1
37
+ return GetEuclidGCD ( Math . abs ( firstNumber ) , Math . abs ( secondNumber ) ) === 1
38
38
}
39
39
40
40
export { CoPrimeCheck }
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments