Simple and elegant

Continuing the discussion from freeCodeCamp Challenge Guide: Smallest Common Multiple:

Found this from the discussion section submitted by a user.

function smallestCommons(arr) {

  var max = Math.max(arr[0], arr[1]);
  var min = Math.min(arr[0], arr[1]);
  var mltple = max;

  for(var i = max; i >= min; i--){
    if(mltple % i !== 0){
      mltple += max; 
      i = max;
    } 
  }

  return mltple;  
}

Need help to explain this please…

Thanks~

2 Likes

what do you need explained?
what do you understand, what does confuse you?

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

sorry but someone can explain me the procces, i understand the min and max metodh but the if inside the for loop dont understand it,if someone can explain me thanks;
i know that this solution is basic and readable but well i want know why/how give the result…

I had a similar thought process for this problem, but couldn’t figure how to write it up until I saw his code (I was trying to use a while loop in the for loop, etc what a mess).

I will break it down how I understood it using smallestCommons ([1,3])
So the min = 1 & max = 3
He has set his variable mltple equal to max (or 3 in this case)

The for loop is created to go 3 --> 2 --> 1 then break

The if statement condition essentially says that the remainder of mltple divided by i does not equal 0 and if that is true then execute the below code which is max is added to mltple and i is reset to max (or 3). If the if statement is false, where the remainder does = 0, then you will decrease i by 1 and run through the loop again.

So starting with i = 3
3 % 3 !== 0 is false --> move to i =2
3 % 2 !== 0 is true --> 3 + 3, i = 3
6 % 3 !==0 is false --> move to i =2
6 % 2 !==0 is false --> move to i = 1
6 % 1 !==0 is false --> for loop breaks because of initial condition

then the code says to return mltple, which is 6 at the time of the for loop breaking.

As you can see, 6 is divisible by all the numbers in the range of 1 to 3. This is why you have to work incrementally from greatest to smallest.

Hope that helps!

10 Likes

it goes through the multiples of max (represented by mltple). if the multiple is not evenly divisible by max to min (represented by i), it goes to the next multiple (mltple += max) and resets i (i = max). when mltple is evenly divisible by max to min, the program ends (return mltple).