# Related to challenge - Smallest common multiple finder

I am getting infinite loop error on line 17 for some reason (It is this line- var divCheck = checkSequentialDivisions(arr, arr * i);). I am unable to find it.

``````function checkSequentialDivisions(arr, scm){
console.log(arr, 'arr', scm, 'scm');
var lowestNum = arr > arr ? arr : arr;
var highestNum = arr > arr ? arr : arr;
for(lowestNum; lowestNum <= highestNum ; lowestNum++){
if(scm % lowestNum != 0){
return false;
}
}
return true;
}
function smallestCommons(arr) {
var scm = false, i = 1;
while(!scm){
if((arr * i === arr * i) || (arr === 1 && arr === 1)){
console.log('in if- scm', arr * i);
var divCheck = checkSequentialDivisions(arr, arr * i);
if(divCheck){
scm = true;
arr = arr * i;
}
}
i++;
}

return arr;
}

smallestCommons([1,5]);
``````

Do you have any questions?

Yes…I just updated the question. Thanks for asking I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text. 1 Like

Add the following console.log statements and you will see why your if statement always evaluates to false which in turn never allows your while loop exit condition to be met.

``````  while(!scm){
console.log(arr * i, arr * i , (arr === 1 && arr === 1) );
console.log(arr === 1, arr === 1  , (arr === 1 && arr === 1) );
if((arr * i === arr * i) || (arr === 1 && arr === 1)){
``````

When using while it is expected for the condition to change over time, to give you a break from the loop. scm stays the same as i is 1, so arr is equivalent to `arr`, so checkSequentialDivisor function returns the same results every time. Also, if i is 1 and `arr` is equivalent to `a`, shouldn’t arr be an array. `a[0`]` does not seem one.

Thanks…I too was wondering how can I format the code in a more readable way! I got it…the condition itself is wrong. What I did was multiplied both the numbers with same loop number and expected to be same…it cannot be true however…

Thanks guys I solved the challenge. My solution was not efficient enough.Used the algorithm described in many other solutions.

``````
function checkSequentialDivisions(newArr, scm){
for(var i = 2 ; i < newArr.length ; i++){
if(scm % newArr[i] != 0){
return false;
}
}
return true;
}

function smallestCommons(arr) {
var scm = false, i = 1;

arr.sort(function(a, b){
return b-a;
});

var newArr = [];
for(var j = arr ; j >= arr ; j--){
newArr.push(j);
}

while(!scm){
var divCheck = checkSequentialDivisions(newArr, newArr * newArr * i);
if(divCheck){
scm = true;
arr = newArr * newArr * i;
}
i++;
}

return arr;
}

smallestCommons([1, 5]);

``````