Possible infinite loop only when passing larger numbers

My code for the Smallest Common Multiple challenge works fine with smaller numbers, but for some reason when larger numbers are used, it tells me that I have a possible infinite loop. I think the code should work with any set of numbers. Why would there be an infinite loop with larger numbers? Like when I pass 1 and 11 it works fine, but when I pass 1 and 12, it says there’s a possible infinite loop. Can anyone offer any words of advice for this noob?

  **Your code so far**

function smallestCommons(arr) {
  if (arr[0] > arr[1]) {
    arr = [arr[1], arr[0]];
  let newArr = [];
  for (let i = arr[0]; i <= arr[1]; i++) {
  let x = newArr.length - 1;
  let num = newArr[x] ;
  let testArr = [-1];
  while (testArr.some(x => x !== 0)) {
    if (testArr[0] === -1) {
    testArr = [];
    for (let i = 0; i < newArr.length; i++) {
      testArr.push(num % newArr[i]);
  num = num - 1;
  return num;

console.log(smallestCommons([1, 6]));
  **Your browser information:**

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

Challenge: Smallest Common Multiple

Link to the challenge:

This means that your code takes too long to run.

You can fix this by either

  1. Using a better algorithm (like the GCD based approach on Wikipedia)
  2. Reducing the inefficiency in your code

For example, making this range isn’t needed and is inefficient.

and constantly pushing and popping off this array isn’t very efficient.

And stepping by 1 is a good choice

Your variable scoping is strange, by the way.

Ok, thanks for the advice. I’ll get to work on fixing the inefficiency and the variable scoping.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.