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: