# Why is my code slow? Smallest Common Multiple challenge

**URL:** <https://forum.freecodecamp.org/t/why-is-my-code-slow-smallest-common-multiple-challenge/105557>\
**Category:** General\
**Created:** [April 13, 2017, 7:25am UTC](https://forum.freecodecamp.org/t/why-is-my-code-slow-smallest-common-multiple-challenge/105557 "2017-04-13T07:25:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagayev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sagayev/32/84336_2.png) [@sagayev](https://forum.freecodecamp.org/u/sagayev)\
**Post date:** [April 13, 2017, 7:25am UTC](https://forum.freecodecamp.org/t/why-is-my-code-slow-smallest-common-multiple-challenge/105557/1 "2017-04-13T07:25:54Z")

</div>

The code below seems to work correct, but I can barely finish for [1,2] and [1,3]. I havent tried it for big arrays. Could you please comment, what is wrong with this code?

Fir I am creating reverse array of numbers between max and min.

Then I use arrar.reduce to calculate LCMs.

```
 function smallestCommons(arr) {
      var marr=[];
      var mi=Math.min.apply(null,arr);
      var ma=Math.max.apply(null,arr);
  
  for (var a=ma; a>=mi; a--){marr.push(a);}
  
  return marr.reduce(function lcm(a, b) {
   var aa=arguments[0];
   var bb=arguments[1];
   while(true){ 
     if (aa%b===0){return aa;}
     aa=aa*2;
     if (bb%a===0){return bb;}
     bb=bb*2;
   }
});
  
}

smallestCommons([1, 2]);
```

---

<div class="post-metadata">

**Author:** ![JohnL3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnl3/32/16459_2.png) [@JohnL3](https://forum.freecodecamp.org/u/JohnL3)\
**Post date:** [April 13, 2017, 10:37am UTC](https://forum.freecodecamp.org/t/why-is-my-code-slow-smallest-common-multiple-challenge/105557/2 "2017-04-13T10:37:34Z")

</div>

unfortunately your code dosent work … eg numbers 8,5 your marr would be [8,7,6,5]  
you then check 8%7 and if not 0 go 8\*2 =16 check 16%7 etc till gets to 56 then its 0  
while also checking 7  
when 56 it then changes to numbers and uses 56 and 6 so you get 12%56 24%56 etc and never finds one that gives 0 and you have 112%6 224%6 etc and never finds one that gives 0 … you are now stuck in a infinite loop

you need to change from the reduce function down and work out first how to get the scm and then code it … i would suggest googling how to get smallest common multiple … understand how its obtained and then you will find it easier to code a sloution.  
if you are wondering how i can see what is happening in your code … i created a var count =0 at the beginning of your code … then i changed your while to **while(count \<100){** … added **count ++;** after your last bb=bb\*2; … and then put console.logs in your if statements to console.log(aa +’ '+ b) and the same for bb a …  
doing this i could see what is going on for the first 100 loops and what the values are changing too and where is the problem

---

<div class="post-metadata">

**Author:** ![sagayev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sagayev/32/84336_2.png) [@sagayev](https://forum.freecodecamp.org/u/sagayev)\
**Post date:** [April 13, 2017, 10:52am UTC](https://forum.freecodecamp.org/t/why-is-my-code-slow-smallest-common-multiple-challenge/105557/3 "2017-04-13T10:52:44Z")

</div>

JohnL3,

Thank you for reviewing the code also for valuable hint how to debug the while loop.

---

<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:** [January 17, 2021, 8:52pm UTC](https://forum.freecodecamp.org/t/why-is-my-code-slow-smallest-common-multiple-challenge/105557/4 "2021-01-17T20:52:53Z")

</div>

Closed on January 17, 2021, 8:52pm UTC


