Find the Greatest Common Divisor
Chinwendu Agbaetuo
Posted on October 21, 2024
Write a function that takes two numbers and returns their greatest common divisor (GCD).
Solution
function findGCD(number1, number2) {
if(number2 === 0) {
return number1;
}
return findGCD(number2, number1 % number2);
}
console.log(findGCD(-1, -5));
console.log(findGCD(19, 5));
console.log(findGCD(72, 81));
console.log(findGCD(14, 0));
Result
> -1
> 1
> 9
> 14
💖 💪 🙅 🚩
Chinwendu Agbaetuo
Posted on October 21, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.