Tell us what’s happening:
Your code so far
function smallestCommons(arr) {
var range = [];
for (var i = Math.max(arr[0], arr[1]); i >= Math.min(arr[0], arr[1]); i--) {
range.push(i);
}
// can use reduce() in place of this block
var lcm = range[0];
for (i = 1; i < range.length; i++) {
var GCD = gcd(lcm, range[i]); // :wave: Question1 : Does this mean storing a function which is called gcd to a variable named GCD??
lcm = (lcm * range[i]) / GCD;
}
return lcm;
function gcd(x, y) { // Implements the Euclidean Algorithm
//:wave: Question 2 ::Is this gcd(x,y) same as the gcd((lcm, range[i]) above?
if (y === 0)
return x;
else
return gcd(y, x%y);
}
}
// test here
smallestCommons([1,5]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
.
Link to the challenge:
I listed my questions in the code. I would be grateful if someone can explain the code for me…