# Smallest Common Multiple infinite loop

Smallest Common Multiple infinite loop
0

#1

Hey guys (and girls)…

I have a code:

``````function smallestCommons(arr) {

arr = arr.sort((a, b) =>  a < b);

var arr2 = [];

var count = 0;

for(let i = 0; i <= arr[0] - arr[1]; i++) {
arr2.push(arr[0] - i);
}

for(let i = arr2[0]; i < 10000; i += arr2[0]) {
for(let j = 0; j < arr2.length; j++) {
if(i % arr2[j] !== 0) {
count = 0;
break;
}else if(i % arr2[j] === 0) {
count ++;
if(count === arr2.length) {
return i;
}
}
}
}

}

smallestCommons([1,5]);
``````

and I know putting 10000 in a for loop isn’t right but i did it just to experiment and the code works well if result isn’t bigger than 10000 (first 3 tests). How to set that condition in order to avoid infinite loop but get all test correct?

#2

Well, how should your code know when to stop looping?

#3

I have no idea how to tell it to stop…

#4

Logically, if you were telling a person how to do this task when would they stop?

#5

Wouldn’t stop until the solution was found…

count <= arr2.length;

it works now

#6

Congrats! I knew you were almost there on your own!