Smallest Common Multiple - Inconsistent results

Smallest Common Multiple - Inconsistent results
0

#1
function smallestCommons(arr) {
  if(arr[1]<arr[0]){
    var temp = arr[0];
    arr[0] = arr[1];
    arr[1] = temp;
  }
  var match = 1;
  var found = false;
  var c = 0;
  while(found==false){
   for(i=arr[0];i<=arr[1];i++){
     if(match%i==0){
       c++;
     }
   }
   if(c==(arr[1]-arr[0]+1)){
     found = true;  
    }else{
      match++;
      c = 0;
    }
  }
  console.log(match);
  return match;
}

When I run this the first 3 checks pass but the other two don’t, while showing different values for match each time. Why does this happen? What am I doing wrong?


#2

Hi,

I think your code works but it might be timing out. If your code runs long FCC just assumes you have an endless loop and halts execution. I ran your code in developer console and it ran to completion with correct answers.

You can bypass that endless loop protection in FCC if you are confident in the code. (There is also solution at link showing what to do if you have an endless loop that prevents you from returning to the problem page.)

That said, if your code is running that long it may need some tweaking.

Good luck. That’s a hard one.


#3

Thanks! I defined match as arr[1] and then I incremented it by arr[1] so that it converges faster.