Smallest Common Multiple failing at [25,18]

Smallest Common Multiple failing at [25,18]
0.0 0

#1

Tell us what’s happening:
my code works for the 4 but only fails in [25,18].
When I copy and paste [25.18] to the console. it says this could create an infinite loop/
I can’t figure out why it works for [5,1] but not for [25,18].
Is it because it goes througth the loop so many times even though it’s not infinite loop?

Your code so far

function smallestCommons(arr) {
  var num;
  var i;
  var temp=[];
  if(arr[0]<arr[1]){
   for(num=arr[1];num<1000000;num++){
    for(i=arr[0];i<=arr[1];i++){
       if( (num % i) !=0)
         break;    
    }
    if( (num % i)==0 )
     return num;
   
   } 
    
 }
  
  if(arr[1]<arr[0]){   
  for(num=arr[0];num<6500000;num++){
    for(i=arr[1];i<=arr[0];i++){
       if( (num % i) !=0)
         break; 
    }
   if( (num % i)==0 )
     return num;
   
   } 
  
  
  
}

}
smallestCommons([1, 13]);

Your browser information:

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

Link to the challenge:


#2

You could use Number.MAX_SAFE_INTEGER if you wanted to use the largest possible number that’s safe to use in JavaScript, instead of arbitrarily setting the upper limit to 1000000 or 6500000 or whatever.

Even with using MAX_SAFE_INTEGER, which would result in:

smallestCommons([25, 18]);
787386600

this is not the smallestCommon (should be 6056820).

However, the number you choose to end the looping isn’t the real problem. I’d double check the logic and see if you really need that many loops and what/why you are doing what you are doing inside the loops. To be honest, this [25, 18] case is not just “the last test case”, it’s the most important one, and you’ve just been lucky that it has been passing the previous tests based on the tests they’ve chosen to use on this site, which happen to all have the number 1 in them.