# Infinite Loop

I’ve been following Mr. Snow’s tutorial on Least Common Multiple… but his finished solution leaves me with an infinte loop on line 17. Can anyone explain to me what is going on here?

https://youtu.be/e5_DelH7a_A

``````function smallestCommons(arr) {
// set LCM boolean;
// if yes,throw boolean and return LCM;
var first = Math.min (arr[0],arr[1]);// sort array of (arr);
var last = Math.max (arr[0],arr[1]);
var range = [];

for(var i = first; i <= last; i++){
range.push(i);
}

var LCM = 0;
var flag = true;

while (flag){
LCM++;
for(var j = first; j <= last; j++){

if (LCM % j !== 0){
break;
}
else if (j===last){
flag = false;
}
}
}
return LCM;}// end of smallestCommons(arr);
smallestCommons([23, 18]);
``````

The while loop has LCM increasing each time through, so it will eventually end when LCM is a number that has no remainder when divided by each of the numbers that you supply.

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