# Greatest common divisor

hi guys.

was trying to solve a gcd and scm code challenge but didnt seem to get it.So I saw this code online but dont seem to understand what the gcd and scm functions are doing or even what the a and b parameters are

``````function smallestCommons(arr) {

var max = Math.max(...arr),
min = Math.min(...arr),
range = [],
scm; // smallestCommonMultiple

for (var i = min; i <= max; i++) {
range.push(i);
}
function gcd(a, b) {  // greatestCommonDivisor based on Euclid's Algorithm
for (var x = 0; x <= a; x++) {
if(a % x === 0 && b % x === 0)
}
}
function lcm(a, b) {  // leastCommonMultiple based on Euclid's Algorithm
return (a * b) / gcd(a, b);
}
scm = range.reduce(lcm);

return scm; // smallestCommonMultiple
}
smallestCommons([23, 18]);
``````

Two questions

1. Have you read the Wikipedia articles on GCD and LCM?

2. What kind of math background do you have?

Side note: don’t use var

Yeah, I have read the articles. And my math background isnt all that bad at all. I dont know why wrapping my head around it seem difficult lol

Well, the key first two steps are

1. write a function that finds the GCD of two numbers

2. write a function that finds the LCM of two numbers

So let’s start there. The GCD algorithm presented on Wikipedia is recursive while this one is not. Cn you write the recursive version? Do you understand the algorithm?

No , I don’t understand the algorithm.

Okay , I reread the article again and I think I understand it a little more for now,I’d let you know if there’s any more problem. Thanks alot!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.