"Smallest Common Multiple" passings all cases BUT the LAST one

I made the exercise “Smallest Common Multiple”:

function isDiv(num, ini, fim) {
  var divFlag = true;
  
  for(var i = ini; i < fim; i++) {
    if(num % i !== 0) {
      divFlag = false;
      break;
    }
  }
  return divFlag;
}

function smallestCommons(arr) {
  var bigger = Math.max(arr[0], arr[1]);
  var smaller = Math.min(arr[0], arr[1]);
  var sc = bigger;
  var k = 1;
  
  while(!isDiv(sc, smaller, bigger)) {
    sc = bigger * k;
    k++;
  }  
  return sc;
}

smallestCommons([23,18]);

It works for all cases, but the last one it doesn’t pass. It is very strange because I’ve tested this code on Online Javascript Editor and it worked for all cases ( https://js.do/code/147068 ). What is happening? Is the problem with FCC ?

I’ve discovered the problem. The last case doesn’t work because of an infinite loop protection of FCC. So, I just did what FCC recommeded: write a commet “// noprotect” at the beginning of code to desable the FCC loop infinite protection.

Happy you got it figured out, but it’s worth noting that that infinite loop warning is very important and should be properly addressed

The condition in your while loop has the potential to be satisfied forever. In the real world, this would be very dangerous code (hence the warning).

It may be worthwhile to try to refactor your code so that you no longer have that inifinite loop potential.

1 Like