Smallest Common Multiple challenge

Smallest Common Multiple challenge
0

#1

hello campers ,I don’t know why I got everthing correct in google chrome console , but in FCC console the last two tests didn’t pass , is this a bug?

function smallestCommons(arr) {
    var max=Math.max.apply(null,arr);
    var min=Math.min.apply(null,arr);
    let Range=[];
// find the range 
    for (let i=min;i<=max;i++){
        Range.push(i);
    }
//find the first and last arguments 
let firstNum=arr[0]; //1 
let secondNum=arr[1]; //5 
 for (let i=0;i<Infinity;i++){
     let stopLoopFirst=0;
     let stopLoopSecond=0;
     firstNum+=arr[0];
     secondNum+=arr[1];
        for(let i =0;i<Range.length;i++){
            
            let firstNumDivision=firstNum/Range[i];
            let secondNumDivision=secondNum/Range[i];
            
     if(firstNumDivision % 1===0){
          stopLoopFirst+=1;
      }
    else if(secondNumDivision % 1===0){
          stopLoopSecond+=1;
      }
            
    }
     if(stopLoopFirst===Range.length){
         return  firstNum;
     }
     else if(stopLoopSecond===Range.length){
          return  secondNum;
     }
 }
}


smallestCommons([1, 13]);

#2

Because the algorithm your code implements is not very efficient, those last two tests trigger FCC’s bulit-in infinite loop protection, which stops the processing of your code early.

Think about how you could make a more efficient solution. See if there is anything in your algorithm steps which might be repetitive or unnecessary looping. If you can not figure how to rework your current solution, there are a few different ways to solve this challenge using a more efficient algorithm. I suggest researching the following two methods on finding the least common multiple

  1. Reduction by the greatest common divisor

  2. By prime factorization


#3

Do you have any suggestions for how to optimize the calculation of a Collatz sequence? I’m hitting the loop protection even though this solution solves locally in only 110ms.