[solved]Smallest Common Multiple Issue (possible spoilers)

[solved]Smallest Common Multiple Issue (possible spoilers)
0

#1

I’m on Smallest Common Multiple and I’m pretty sure the code should work but it keeps saying there’s an infinite loop in the while statement.

Could someone please explain to me what the issue is?

function smallestCommons(arr) {
  var arr1 = arr.sort(function(a,b){
    return a-b;
  });
  var arr2 = [];
  var loopStop = 20;
  var ans = 0;
  
  
    for (var i = arr1[0]; i <= arr[1];i++) {
      arr2.push(i);
    } 
  
  
 
  while (loopStop == 20) {
  
   for (var n = arr2[0]; n <= arr2[arr2.length-1]; n++) {
   if (ans % n !== 0) { 
     ans++;
     break;
   }
   else if (n == arr2[arr2.length-1]) {
   loopStop = 21;
   }
 }
 }
  return ans;
}


smallestCommons([23,18]);


#2

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

It’s possible that your code is just slow enough that the loop protection thinks there’s an infinite loop (essentially it is timing out). You can turn loop protect off at your own risk.


#3

You were right. Every time I’ve tried that before there’s always been a loop and I’d grown to trust it a bit too much I guess. Thank you.


#4

It’s good to be very cautious about turning loop protect off! This challenge can be solved efficiently enough that you don’t need to, but I believe most students end up turning off loop protect on this one.