Judge My Code: Intermediate Algorithm Scripting: Smallest Common Multiple

Hey guys having an issue with this challenge: all tests pass except for smallestCommons([2, 10]) - which returns 20 for some reason instead of 2520. By the way, smallestCommons([1, 10]) returns 2520. Can you take a look at my code and give me some general feedback along with how to fix this problem? Regards.

function smallestCommons(arr) {
  let sorted = arr.sort();
  let multiples = [];
  for(let i = 1; i < 1000000000; i++) {
    multiples.push(arr[0] * arr[1] * i);
  for(let i = 0; i < multiples.length; i++) {
    for(let j = sorted[0]; j < sorted[sorted.length - 1]; j++) {
      if((multiples[i] / j) % 1 !== 0) {
        continue outer_Loop;
    return multiples[i];

console.log(smallestCommons([2, 10]));

In two words: “Holy Hell!”

To be clear, I’m not trying to discourage you, I’m just wondering about the logic that made you come up with this algorithm. Especially that million-member array wherein 999,999 data values are the non-answer. Yikes. I think you blindly sorta figured that the LCM of the range of numbers between arr[0] and arr[1] must be a multiple of their product, and then decided to test each of the first 1 million such numbers one by one against every number in the range. I’m going to point out two popular ways of getting the LCM. The proven fastest is the Euclidean algorithm. I’m also partial to prime factorization.

If I wanted to “fix” the problem you’re having, I’d point out that you’re not passing a callback function to arr.sort(), so Array.prototype.sort()'s default functionality takes over: lexicographic sort. You just got lucky with the other tests that the numbers happen to be in numerical order when you sorted them. However, “2” comes after the “1” in “10.” You definitely need to fix this, but it’s not the only thing about this algorithm you should fix.

Edit: Oh, and one more thing. I saw this post earlier, didn’t see a link to the relevant challenge, and passed on helping you. I only came back because someone else was stuck on it, too, and they included the link, thereby making it easy for me to help them (and thus you.)

Good luck. Make sure to reply to me when you’ve rewritten your code so I can review it again.