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?

function smallestCommons(arr) {
// set LCM boolean;
// LCM++ and check answer;
// 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++){
  var LCM = 0;
  var flag = true;
  while (flag){
    for(var j = first; j <= last; j++){
      if (LCM % j !== 0){
      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.