Smallest Common Multiple using .every()

Hi there, I wanted to try and solve this using a different approach (I know the while loop is really inefficient here) It is providing a correct answer for all the other conditions except the last. Can anyone tell me why this is failing?

Your code so far


function smallestCommons(arr) {
  //sort array low to high
  arr.sort(function(a, b) {
    return a - b;
  });
  console.log(arr)
  //create full array
  var allNumbers = [];
  for (var i = arr[0]; i <= arr[1]; i++) {
    allNumbers.push(i);
  };
  var commonNumber = 2; 
  //callback function to test each number in allNumbers array
  function testInt(el) {
    return Number.isInteger(commonNumber / el);
    };
  //loop increments commonNumber until all numbers in array divided by it are integers
  while (allNumbers.every(testInt) === false) {
      commonNumber++;
  };

  console.log(commonNumber);

  return commonNumber;
  //console.log(checkInt);
}


smallestCommons([1,13]);

Your browser information:

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

Link to the challenge:

As you pointed out, it isn’t a very efficient solution. That means it takes a very long time to run (relatively speaking) as inputs get large. Usually long-running code is caused by an infinite loop or infinite recursion. To protect campers from crashing their browsers, FCC has an execution time limit. For the last test case, your solution takes enough time to exceed that limit.

1 Like

Thanks Ariel, that makes sense, I appreciate your feedback