Smallest Common Multiple Test Case Bug

Smallest Common Multiple Test Case Bug
0

#1

Tell us what’s happening:
I debugged the code in Chrome Inspect element and the code passes all the test cases mentioned below the question. When I run the code in Chrome, All the answers match the expected answers, however when I run the code in FreeCodeCamp’s test environment, the last test case fails for some reason. Is this a known problem ?

I do not see any flaw in my algorithm. Please feel free to correct me if I am wrong.

Note: The answer to the last test case smallestCommons([23, 18]) is 6056820, which is what my code achieves too, however for some reason the test case fails in FreeCodeCamp’s test environment.

Your code so far

function smallestCommons(arr) {
  arr.sort();
  var flag = 1, num = arr[1];
  while(flag != 2){
    for(var i = arr[0]; i <= arr[1]; i++){
      if(num%i != 0){
        flag = 0;
        break;
      }
    }
    if(flag){
      flag = 2;
    }
    else{
      flag = 1;
      num++;
    }
  }
  return num;
}


smallestCommons([1,5]);

Your browser information:

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

Link to the challenge:


#2

If you tried passing in [1,13], you’ll see this error:

image

If you’re pretty sure about your code, you can add // noprotect at the beginning of your code to disable loop protection (spoilers: your code works well)


#3

Thanks for the Response @kevcomedia . I got the code working. I disabled loop protection with //noprotect. Here is the final code:

function smallestCommons(arr) {
  arr.sort(function(a,b){return a-b;});
  alert(arr);
  var flag = 1, num = (arr[1]%2 === 0)?arr[1]:(arr[1]+1);
  while(flag != 2){
    //  noprotect
    for(var i = arr[0]; i <= arr[1]; i++){
     //   alert(i);
      if(num%i !== 0){
        flag = 0;
        break;
      }
    }
    if(flag){
      flag = 2;
    }
    else{
      flag = 1;
      num = num + 2;
    }
  }
  return num;
}


smallestCommons([18,23]);