FCC Challenge: Smallest Common Multiple


developer tool shows my code as correct. but fcc console doesnt accept my solution.

function smallestCommons(arr) {
  if(arr[0]>arr[1]){arr=[arr[1],arr[0]]};
  let nums=[];
  for(let i=arr[0];i<=arr[1];i++){
    nums.push(i);
  }
  let ctr=0;
  for(let i=nums[0];true;i++){
    for(let j in nums){
      if(i%nums[j]==0){ctr++;}
    }
    if(ctr==nums.length){return i;}   
    ctr=0; 
  }
}

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

What do the failing tests say? Is your code running too slowly?

You never want to do this because a) it’s an indication that your code will be super slow, b) it is an indication that you’re using the wrong type of loop.


Could you please edit your title to be something meaningful?

Hi. I understand. I need to think of a new approach to the problem. Also, what is the “correct” type of loop if I were to pursue this kind of approach?

I have edited the title as well.

For this problem, a while loop or a for...in loop are good fits.