Could you please help why this code have an infinite loop?
function smallestCommons(arr) {
var lst = [];
for (var m = Math.min(arr[0], arr[1]); m <= Math.max(arr[0], arr[1]); m++) {
lst.push(m);
}
var i = lst.reduce(function(a,b){return a*b;});
var k = lst.reduce(function(a,b){return a*b;});
while (i > 1) {
for (var j = lst[0]; j <= lst[lst.length-1]; j++) {
if (i%j !== 0) {
break;
} else {
k = i;
}
}
i--;
}
return k;
}
I copy-pasted your code as-is in a chrome devtools snippet - it runs fine and terminates - can you try the same? It may be a problem in the environment of the exercise tab
I’ve tried the code using Chrome and Safari, and neither triggers the infinite loop warning. Keep in mind that this is a precaution and it doesn’t mean your code is actually running in an infinite loop. Try refreshing your browser.
yes - 6 is right - I pasted the wrong value - the code does run and return a value - I think on the exercise page the test case smallestCommons([1,13]) is very slow because i is 6227020800 and the nested loop takes so long it seems an infinite loop though it does eventually terminate - you should improve your solution