Smallest Common Multiple Large Numbers are Wrong

Tell us what’s happening:
So, this is a little odd. I’m getting correct values until the numbers get very large. I’m guessing this has to do with floating point processing, but when I use the answer code it works fine. In theory, I think my code should work, so I’m a little confused where my inefficiency is.

any help is appreciated.

EDIT: Yeah, I solved it. in the while, loop, I just changed:

ans = index * Math.min(...newArr);


ans = index*newArr[0]*newArr[1];

I guess the time limit must be really strict, as I also made a bunch of other small optimizations in order to fit in.

Your code so far

function smallestCommons(arr) {
  arr = arr.sort((a,b) => a-b);
  var newArr=[];
  var found = false;
  var ans = 0;
  var index = 0;

  for(var i = Math.max(...arr); i >= Math.min(...arr); i--){

    ans = index * Math.min(...newArr);
    for(var i = 0; i < newArr.length; i++){
      if(ans % newArr[i] !== 0){
        found = false;
      }else if(i===newArr.length-1){
        found = true;
        return ans;
return ans;


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

Link to the challenge:

FreeCodeCamp has an infinite loop protection : once a loop need too much time to execute it is stopped - you may be triggering this because your code is not efficient enough

Thanks for the advice. I’m not sure if it was infinite loop protection, but it was a timing issue. I changed a few lines to make it faster and it works now. I’m just surprised such a small difference has such an impact.