Should this be happening?

“Potential infinite loop detected on line 11. Tests may fail if this is not changed.”

The test should not fail once the expected result is returned. Am I right?

  **Seu código até o momento**

function smallestCommons(arr) {
  const numberSmaller = arr[0] > arr[1] ? arr[1] : arr[0];
  const numberLarger = numberSmaller === arr[0] ? arr[1] : arr[0];
  const interval = [];
  let i = numberSmaller;
  while (i <= numberLarger) {
    interval.push(i);
    i++;
  }
  let count = 1;
  while (true) {
    const match = interval.map(number => count % number)
      .every(number => number === 0);
    if (match) {
      return count;
    }
    count++;
  }
}

// smallestCommons([1,5]);
console.log(smallestCommons([1, 5])); // 60
console.log(smallestCommons([5, 1])); // 60
console.log(smallestCommons([2, 10])); // 2520
console.log(smallestCommons([1, 13])); // 360360
console.log(smallestCommons([23, 18])); // 6056820
  **Informações de seu navegador:**

Agente de usuário: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0

Desafio: Encontrar o menor múltiplo comum

Link para o desafio:

If you remove the console logs you won’t see that message. But it shouldn’t fail either, at least it doesn’t for me.

I added the consoles just to illustrate the expected result, without them the error still persists.

// executando testes

smallestCommons([23, 18])

retornará 6056820.

// testes concluídos
// saída do console
Potential infinite loop detected on line 11. Tests may be failing because of this.

I just meant to remove all the function calls it doesn’t matter if they are inside console.log calls. So if you remove everything except the function definition the message should go away.

Not sure how or when (under what condition) the message gets generated. It’s just some arbitrary approximation of time passed as a cut-off point, you can’t actually know if code is in an infinite loop (that would literally take forever and never result in an answer).

Edit: I guess you can do static code analysis and make a good guess as to if the code is in an infinite loop state. But you still can’t physically prove it by waiting.

Sorry, I don’t quite understand what you mean, but I removed all the function calls, leaving just the declaration, and it worked.

Even working, this code is not right, I’ll look for another way to solve this issue.

Thanks for the help sir.

Sorry if I was confusing you with the extra stuff, I just meant to say you can’t prove something is infinite, loop, or otherwise. You can only guesstimate it.

It’s irrelevant anyway, there is some infinite loop protection and warning that is generated using some condition. Apparently having the function calls triggers it.

I imagine the ‘while (true)’ sets off alarm bells. If the function never gets called it’s irrelevant.

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