# Simple and elegant

**URL:** <https://forum.freecodecamp.org/t/simple-and-elegant/361087>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 20, 2020, 5:32am UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087 "2020-03-20T05:32:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CoDean](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codean/32/132892_2.png) [@CoDean](https://forum.freecodecamp.org/u/CoDean)\
**Post date:** [March 20, 2020, 5:32am UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087/1 "2020-03-20T05:32:37Z")

</div>

Continuing the discussion from [freeCodeCamp Challenge Guide: Smallest Common Multiple](http://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/9):

Found this from the discussion section submitted by a user.

```auto
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~

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 20, 2020, 8:39am UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087/2 "2020-03-20T08:39:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 20, 2020, 8:40am UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087/3 "2020-03-20T08:40:47Z")

</div>

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](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) 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](https://sea3.discourse-cdn.com/freecodecamp/uploads/default/original/3X/4/b/4b9a26f0c243380dd84935f4e29b92e3ecee8d05.gif)

---

<div class="post-metadata">

**Author:** ![barriosmiguel169](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/barriosmiguel169/32/134562_2.png) [@barriosmiguel169](https://forum.freecodecamp.org/u/barriosmiguel169)\
**Post date:** [May 18, 2020, 7:50pm UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087/4 "2020-05-18T19:50:02Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![kdshah6593](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kdshah6593/32/137685_2.png) [@kdshah6593](https://forum.freecodecamp.org/u/kdshah6593)\
**Post date:** [June 16, 2020, 10:38pm UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087/5 "2020-06-16T22:38:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tetodrill](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tetodrill/32/134310_2.png) [@tetodrill](https://forum.freecodecamp.org/u/tetodrill)\
**Post date:** [November 8, 2020, 6:12am UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087/6 "2020-11-08T06:12:35Z")

</div>

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`).

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 10:03am UTC](https://forum.freecodecamp.org/t/simple-and-elegant/361087/7 "2021-06-01T10:03:14Z")

</div>

Closed on June 1, 2021, 10:03am UTC


