Help with Smallest Common Multiple and Infinite Loop

Help with Smallest Common Multiple and Infinite Loop
0

#1

I am wondering why this code is returning an infinite loop…

function smallestCommons(arr) {
  var maxNum = Math.max.apply(null,arr);
  var minNum = Math.min.apply(null,arr);
  
  var rangeSeq = [];
  
  
  for (var i = minNum; i <= maxNum; i++) {
    rangeSeq.push(i);
  }
  
 
  var multiple = 0;
  
  var checkFunc = function(b) {
    return b === 0;
  };
  
  var remain = rangeSeq.map(function(a) {
    return multiple % a;
  });
  
  do {
    multiple += multiple;
  }
  while (remain.every(checkFunc) === false);
  
  return multiple;
}


smallestCommons([1,13]);

#2

Not seeing an infinite loop with your code. Your function does return zero for every test case though.


#3

When multiple is 1, it returns a warning for an infinite loop. Also, why is it returning 0?


#4

Put the following console.log statement right after the assignment to remain with the map:

  var remain = rangeSeq.map(function(a) {
    return multiple % a;
  });
  console.log(remain); // display the array of zeros here

remain is an array of zeros. Why? Because you start off with multiple = 0. Inside the map callback function you return multiple %strong text a for each number in rangeSeq. Zero mod any number greater than zero is 0. That is why remain is an array of zeros.


#5

Which test specifically is warning of infinite loop?