Smallest Common Multiple [I do not understand the example code]

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…

Hi,

Answer to Q1: here you store the output of the function gcd(x,y) to the variable named CGD. Since the output of the function is just a number, the typeOf GCD is "number".

Answer to Q2: Yes, it is. On this step you have declared the function and the set of things it is going to perform on values x and y. When you call the gcd(lcm, range[i]) the parameters lcm and range[i] play the same role as x and y Since the function is declared in a way like:

function functionName(parameters) {
    // your code here
}

It doesn’t matter when such function was declared (the order doesn’t matter) that is why it can be invoked even before the declaration like in case of var GCD = gcd(lcm, range[i]);

Thank you so much!
I kind of understand now, but not fully understand yet…

It doesn’t matter when such function was declared (the order doesn’t matter) that is why it can be invoked even before the declaration like in case of var GCD = gcd(lcm, range[i]);

This is what confuses me the most.

So… in the for loop

for (i = 1; i < range.length; i++) {
    var GCD = gcd(lcm, range[i]);  
    lcm = (lcm * range[i]) / GCD;
    }
    return lcm;

The first time go as var GCD = gcd(lcm, range[i])
Now GCD = gcd(5, 4) and it will go with

function gcd(x, y) {  
    if (y === 0)
        return x;
    else
        return gcd(y, x%y);
    }
}

Now it is

if (4 === 0)
  return x;
else
 return gcd (4, 1) //  5%4 ===1

And then it goes to

lcm = (lcm * range[i]) / GCD; // lcm = (5 * 4) / GCD
what is the value of GCD now?

I am also lost for the rest of the steps…

Hi,

Well, if this is related to my words regarding the function declaration, I suggest you to google smth related to function declaration hoisting in JS or you can find a bit info in the documentation - MDN


Regarding the gcd(x,y).

If you throw to it 5 and 4 how it will work:

  1. invoked gcd(5,4)
  2. invoked gcd(4,1)
  3. invoked gcd(1,0)
  4. return 0

it will stop working only if the following condition is true:

 if (y === 0)
        return x;

else it will invoke itself one more time

else
   return gcd(y, x%y);

it goes to this step only when the gcd(x,y) function returns the x. It will do so only when the y === 0

to better understand how the gcd(x,y) works you have to be aware with recursion

1 Like

I can suggest you to use the console.log(); to better understand what is your code doing.

For exmaple to understnad this part of the code:

    var range = [];
    for (var i = Math.max(arr[0], arr[1]); i >= Math.min(arr[0], arr[1]); i--) {
    range.push(i);
    }

you can use something like below:

let arr = [1, 5];

var range = [];

console.log("Math.max(arr[0], arr[1]) is " + Math.max(arr[0], arr[1]));

console.log("Math.min(arr[0], arr[1]) is " + Math.min(arr[0], arr[1]));

for (var i = Math.max(arr[0], arr[1]); i >= Math.min(arr[0], arr[1]); i--) {
  range.push(i);
  console.log("var i is equal to " + i + ", I am pushing it to range array and it is equal now to [" + range + "]");
}

and you will see step by step how your code is working now:

"Math.max(arr[0], arr[1]) is 5"
"Math.min(arr[0], arr[1]) is 1"
"var i is equal to 5, I am pushing it to range array and it is equal now to [5]"
"var i is equal to 4, I am pushing it to range array and it is equal now to [5,4]"
"var i is equal to 3, I am pushing it to range array and it is equal now to [5,4,3]"
"var i is equal to 2, I am pushing it to range array and it is equal now to [5,4,3,2]"
"var i is equal to 1, I am pushing it to range array and it is equal now to [5,4,3,2,1]"

Looks massive but it is a pretty good approach to see and understand what are the certain values on a certain step of the code execution.

Thank you very much!
It’s not your words that made me confused, it’s just I misunderstand the sequence.
I did use console.log to try to see what’s going on in the code, but couldn’t really get it.
I will google about recursion, not very familiar with it.
Thanks for help!!