Been at this challenge for days. I’ve tried several different methods but I seem to keep running into a infinite loop. Can anyone spot the error? Line 20 just after while(k)
function smallestCommons(arr)
{
var newArr = [];
arr.sort(function(low, high)
{
return high - low;
});
for (var i = arr[0]; i >= arr[1]; i--)
{
newArr.push(i);
}
var k = true;
var mult = 1;
while(k)
{
mult++;
for (var j = newArr[0]; j <= newArr[newArr.length - 1]; j++)
{
if (mult % j !== 0)
{
break;
}
else if ( j == newArr[newArr.length - 1])
{
k = false;
}
}
}
return mult;
}
smallestCommons([1,13]);
It seems you’re trying to loop over the elements of newArr, however what you’ve written doesn’t actually do this.
You’re probably looking for for (var j in newArr) to set j to the actual elements of newArr.
Try writing down what happens to the value of j in your current loop attempt at each iteration, and you’ll see what @camperextraordinaire meant with their comment.