Infinite Loop Detected - how to suppress

Tell us what’s happening:
I get this result: Potential infinite loop detected on line 19. Tests may fail if this is not changed.

How can i suppress the warning;
it hinders me to run the test.

I tested my code through my own emulator server and i get the correct output.

  **Your code so far**

function smallestCommons(arr) {
  arr.sort((a,b)=>a-b);
  //console.log(arr);
  //16 → 16, 32, 48, 64, 80,…
  //20 → 20, 40, 60, 80,…,
  //lcm: 80

  //create containers
  let newArr = [];
  do {newArr.push([])}while(newArr.length<2);

  for (let i=arr[0];i<=arr[1];i++)newArr[0].push(i);
  let running = true;
  let indexReduce = newArr[0].reduce((a,b)=>a+b)-1;
  //console.log(indexReduce);

  const OFFSET = 1000;
  let result;
  while(running){
      for (let x=indexReduce; x<indexReduce+OFFSET;x++){
        newArr[1].push(x);
      }
      //console.log(newArr);
      //we get all the list of number modulo == 0 for each item in newArr[0];
      let unionArr = {}; //object with our count on union item
      let passArr = []; //multiple array
      newArr[0].every((num,index) => {
        passArr.push([]);
        newArr[1].every((numb,inde)=>{
          if (numb%num==0) passArr[index].push(numb);
          return true;
        });
      return true;
      })
      //console.log(passArr);
      //we search that union on every modulo == 0
      //use array with least number of count to compare with the other array in passArr
      passArr[passArr.length-1].forEach(item =>{
        //console.log('item:'+item);
        passArr.forEach((arr,index) =>{
          if (index == passArr.length-1) return; // do not search on self
          //we only gonna use findIndex
          //get count of similar item in union
          if (arr.findIndex(x=>x==item)>-1){
              if(Object.keys(unionArr).findIndex(y=>y==item)==-1) {
                if (index==0) unionArr[item] = 1
              }
              else unionArr[item] +=1;
          }
        })

      });
      //we check if an item in union has a count
      // count == arr[1]-arr[0]
      // if met result will hold that single union item
      Object.keys(unionArr).every(item=>{
        if(unionArr[item]==arr[1]-arr[0]) {
          result = item;
          return false;
          }return true;
      });
    //console.log(unionArr);
    //console.log(passArr);
    //console.log(unionArr);

    //console.log(result!=undefined)
    if (result!=undefined)running=false;
    indexReduce+=OFFSET; //add more if we check every item in newArr[0] has no modulo == 0 in the items in newArr[1]
    newArr[1] = [];
    //running=false;
  }
  //console.log(result);

  return result;
}
//is my processing tooo slow?
console.log(smallestCommons([2, 5]));
console.log(smallestCommons([1, 5]));
console.log(smallestCommons([1, 10]));
console.log(smallestCommons([1, 13]));
console.log(smallestCommons([23,18]));



smallestCommons([1,5]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0

Challenge: Smallest Common Multiple

Link to the challenge:

you can’t suppress the warning, it is telling you that the loop may take too long to execute. if a loop it is taking too long it will be broken, so, as the warning says:

Tests may fail if this is not changed.

1 Like

Hi, ilenia!

Thank you for your swift response.

I optimized my code by using MATH and changed the approach of the whole code.
Please mark this as answered.

you can mark a post as solution for the topic to appear as answered

Thank you. I marked your answer as solution.

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