Smallest Common Multiple unexpected error output, "Potential infiinte loop ..."

Tell us what’s happening:

Why do I receive the following output? This code returns 360360 outside of the FCC site.

Thanks!

// running tests
smallestCommons([1, 13]) should return 360360.
// tests completed
// console output
Potential infinite loop detected on line 14. Tests may be failing because of this.

Your code so far


function range(a, b) {
/* return an array of the numbers between a and b, inclusive */
const low = a < b ? a : b;
const high = a > b ? a : b;
let nums = [];
for (let i = low; i <= high; i++) {
  nums.push(i);
}
return nums;
}

function smallestCommons(arr) {
const divisors = range(arr[0], arr[1]);
for (let i = 2; ; i++) {
  let multiples = divisors.map(n => n * i);
  let common = true;
  for (let j in multiples) {
    for (let k in divisors) {
      if (multiples[j] % divisors[k] !== 0) {
        common = false;
        break;
      }
    }
    if (common) {
      const lcm = multiples[j];
      return lcm;
    } else {
      break;
    }
  }
}
}

smallestCommons([1,5]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Smallest Common Multiple

Link to the challenge:

your loop doesn’t have a stopping condition, and it probably takes a lot of time to execute - if it takes too much time the tests would stop the execution of the function, and not get the desired result

If you are omitting one of the elements of a loop head, that is a sign that you are using the wrong type of loop or that you have a problem with your logic.

1 Like

Thank you, you’re right! I’m working on a solution that doesn’t use a loop this way.