# Finding a solution for LCM challenge

**URL:** <https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 22, 2020, 12:52pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602 "2020-05-22T12:52:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon43350554](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anon43350554](https://forum.freecodecamp.org/u/anon43350554)\
**Post date:** [May 22, 2020, 12:52pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/1 "2020-05-22T12:52:06Z")

</div>

Hello,  
so i was trying to solve the lcm javascript challenge; however, my code seemed to be very inefficient and i got an error. i got an infinite loop.

i finally found this snippet of code. but i didn’t how the first line works. in other words, i didn’t understand the syntax of the first line.  
thanks

```auto
const gcd = (a, b) => a ? gcd(b % a, a) : b;

const lcm = (a, b) => a * b / gcd(a, b);
//Then use reduce on given array of integers:

[1, 2, 3, 4, 5].reduce(lcm); // Returns 60

```

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [May 22, 2020, 12:57pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/2 "2020-05-22T12:57:22Z")

</div>

Well, the first line makes use of [_Arrow Functions_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), the [_Ternary Operator_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator), and [_Remainder Operator_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators).

I suggest you give those a read, and, if you do not understand something specific, report back here 😉

It is a lot of syntax to learn, though. So, do not expect to understand their ins-and-outs.

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 22, 2020, 3:30pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/3 "2020-05-22T15:30:20Z")

</div>

My recommendation is that instead of looking at code solutions and trying to decipher them, look at the logical (mathematical) solution. I like Wikipedia’s. Then translate that into code yourself. You’ll end up with something more efficient and understand your code solution much better. 🙂

---

<div class="post-metadata">

**Author:** ![anon43350554](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anon43350554](https://forum.freecodecamp.org/u/anon43350554)\
**Post date:** [May 22, 2020, 3:46pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/4 "2020-05-22T15:46:37Z")

</div>

I read these articles and I understood what they meant; however, I am still finding a problem in understanding why that will work thus I will try the method the other person suggested.  
Thanks for your help I understood the code.

---

<div class="post-metadata">

**Author:** ![anon43350554](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anon43350554](https://forum.freecodecamp.org/u/anon43350554)\
**Post date:** [May 22, 2020, 3:47pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/5 "2020-05-22T15:47:10Z")

</div>

Ok thanks. I am going to search for methods to find LCM and GCD in math and I will try converting them.  
Thanks for your help.

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 22, 2020, 3:58pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/6 "2020-05-22T15:58:16Z")

</div>

Good luck and happy coding! Don’t be too shy to ask for help or just use us as a sounding board.

---

<div class="post-metadata">

**Author:** ![anon43350554](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anon43350554](https://forum.freecodecamp.org/u/anon43350554)\
**Post date:** [May 22, 2020, 5:08pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/7 "2020-05-22T17:08:49Z")

</div>

so i came up with the following code which uses functions in i think es5. it is a lot easier for me to understand. i guess it is the same as the previous method as the one I sent before. I used the Euclidean Method thanks to your advice on searching wikipedia for methods. this is my code:

```auto
function smallestCommons(arr) {
  let newArr = []
  if (arr[0] > arr[1]) {
    arr = [arr[1], arr[0]]
  }
  //console.log(arr)
  
  for (let i=arr[0]; i<=arr[1]; i++) {
    newArr.push(i)
  }
  //console.log(newArr)
  
  function gcd(a,b) {
    if (a==0) {
      return b
    } else {
      return gcd(b%a, a)
    }
  }

  function lcm(a,b) {
    return (a*b)/gcd(a,b)
  }

  return newArr.reduce(lcm);
}

console.log(smallestCommons([23,18]));

```

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 22, 2020, 5:10pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/8 "2020-05-22T17:10:29Z")

</div>

Congratulations on working it out! Having the curiosity and drive to rework a problem until you feel good about it is definitely the right mindset to have for this field. 😃

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [May 22, 2020, 5:28pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/9 "2020-05-22T17:28:38Z")

</div>

Nice work. That’s pretty close to how I would have coded it myself. One liners are fine and look slick, but I think that your code is pretty clear, especially if you add some comments to help future you remember what you did.

---

<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, 12:53pm UTC](https://forum.freecodecamp.org/t/finding-a-solution-for-lcm-challenge/392602/10 "2021-06-01T12:53:36Z")

</div>

Closed on June 1, 2021, 12:53pm UTC


