Solution correct but not working in FCC editor (smallest common multiple)

Tell us what’s happening:
The FCC editor is telling me my solution doesn’t work for only the last example but it works in my Atom editor and the browser. I can’t work out why. Any help?

Your code so far

function smallestCommons(arr) {
  
  arr = arr.sort(function(a, b) {
    return a - b;
  });

  var scm = function (x) {
    for (var j = arr[0]; j <= arr[1]; j++) {
      if (x % j !== 0) return false;
    }
    return true;
  };

  var result = 0;

  for (var i = arr[0]*arr[1]; i < Infinity; i++) {
    if (scm(i)) {
      result = i;
      break;
    }
  }
  return result;
}


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/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/smallest-common-multiple

image

FCC uses a timeout to protect against infinite loops. If your code takes too long to run it will terminate early to prevent crashing your browser. If you are completely confident that you don’t have an infinite loop, you can turn the protection off.

FYI: all of the challenges have solutions that are efficient enough not to trigger the infinite loop protection.

2 Likes