mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
add type checking
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Problem statement and Explanation : https://en.wikipedia.org/wiki/Greatest_common_divisor
|
Problem statement and Explanation : https://en.wikipedia.org/wiki/Euclidean_algorithm
|
||||||
|
|
||||||
In this method, we have followed the iterative approach to first
|
In this method, we have followed the iterative approach to first
|
||||||
find a minimum of both numbers and go to the next step.
|
find a minimum of both numbers and go to the next step.
|
||||||
@ -12,6 +12,10 @@
|
|||||||
* @returns return a `gcd` value of both number.
|
* @returns return a `gcd` value of both number.
|
||||||
*/
|
*/
|
||||||
const getGcd = (arg1, arg2) => {
|
const getGcd = (arg1, arg2) => {
|
||||||
|
// firstly, check that input is a number or not.
|
||||||
|
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {
|
||||||
|
return new TypeError('Argument is not a number.')
|
||||||
|
}
|
||||||
// Find a minimum of both numbers.
|
// Find a minimum of both numbers.
|
||||||
let less = arg1 > arg2 ? arg2 : arg1
|
let less = arg1 > arg2 ? arg2 : arg1
|
||||||
// Iterate the number and find the gcd of the number using the above explanation.
|
// Iterate the number and find the gcd of the number using the above explanation.
|
@ -8,6 +8,10 @@
|
|||||||
* @returns `Number` n reverse in reverse.
|
* @returns `Number` n reverse in reverse.
|
||||||
*/
|
*/
|
||||||
const ReverseNumber = (number) => {
|
const ReverseNumber = (number) => {
|
||||||
|
// firstly, check that input is a number or not.
|
||||||
|
if (typeof number !== 'number') {
|
||||||
|
return new TypeError('Argument is not a number.')
|
||||||
|
}
|
||||||
// A variable for storing the reversed number.
|
// A variable for storing the reversed number.
|
||||||
let reverseNumber = 0
|
let reverseNumber = 0
|
||||||
// Iterate the process until getting the number is 0.
|
// Iterate the process until getting the number is 0.
|
||||||
|
Reference in New Issue
Block a user