File tree 1 file changed +7
-4
lines changed
1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change 6
6
*/
7
7
8
8
/**
9
- * GetGCD return the gcd of two numbers.
9
+ * GetEuclidGCD return the gcd of two numbers using Euclidean algorithm .
10
10
* @param {Number } arg1 first argument for gcd
11
11
* @param {Number } arg2 second argument for gcd
12
12
* @returns return a `gcd` value of both number.
13
13
*/
14
- const getGcd = ( arg1 , arg2 ) => {
14
+ const GetEuclidGCD = ( arg1 , arg2 ) => {
15
15
// firstly, check that input is a number or not.
16
16
if ( typeof arg1 !== 'number' || typeof arg2 !== 'number' ) {
17
17
return new TypeError ( 'Argument is not a number.' )
18
18
}
19
+ // check that the input number is not a negative value.
20
+ if ( arg1 < 1 || arg2 < 1 ) {
21
+ return new TypeError ( 'Argument is a negative number.' )
22
+ }
19
23
// Find a minimum of both numbers.
20
24
let less = arg1 > arg2 ? arg2 : arg1
21
25
// Iterate the number and find the gcd of the number using the above explanation.
22
26
for ( less ; less >= 2 ; less -- ) {
23
27
if ( ( arg1 % less === 0 ) && ( arg2 % less === 0 ) ) return ( less )
24
28
}
25
-
26
29
return ( less )
27
30
}
28
31
29
- module . exports = getGcd
32
+ module . exports = GetEuclidGCD
You can’t perform that action at this time.
0 commit comments