# Help with Smallest Common Multiple ♾ Loop

Tell us what’s happening:
This code pasts the first 4 tests but fails the other two. A warning/error is raised that says potential infinite loop. So I’m assuming because of big O notation, this code cannot handle certain ranges. If that is the case, that makes sense. But, does anyone see a fix to this particular function?

Your code so far

``````function smallestCommons(arr) {
let sort_a  = arr.sort((a,b)=> a-b)
//sort array due to instruction that states
//arr will not necessarily be in numerical order.
let range = []; //array to hold range

for(let i = 1; i <= sort_a[sort_a.length - 1];
i++){
range.push(i);
}//get range

console.log(range)

let testMul = sort_a[0] * sort_a[1];
//get lcd// between two arr nums

while (!range.every(n => testMul % n == 0)){
testMul += sort_a[1];
}//test the lcd against all nums in range
//if not all factors, add highest num in range
//to testMul, retest until all nums in range
//are factors

return testMul;
}

// smallestCommons([1,5]);

``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36`

Challenge: Smallest Common Multiple

Link to the challenge:

``````function smallestCommons(arr) {
let sort_a  = arr.sort_a((a,b)=> a-b)
//sort array due to instruction that states
//arr will not necessarily be in numerical order.
let range = []; //array to hold range

// LOOK HERE!!!
for (let i = 1; i <= sort_a[sort_a.length - 1];  i++) {
range.push(i);
}//get range

let testMul = sort_a[0] * sort_a[1];
//get lcd// between two arr nums

while (!range.every(n => testMul % n == 0)){
testMul += sort_a[1];
}//test the lcd against all nums in range
//if not all factors, add highest num in range
//to testMul, retest until all nums in range
//are factors

return testMul;
}

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

Are you sure that the loop I labeled with `LOOK HERE` does what you want?

1 Like

arr.sort_a was supposed to be arr.sort
edited in my post for clarity for anyone that catches that error in your reply

As far as I can tell yes that loop provides the range of values between
sort_a[0] and sort_a[1]. I added a console.log to verify. That console.log given an array [1,5] produces [1,2,3,4,5]. Am I missing something?

wait DOH hold up
i should equal sort_a[0] not 1
gonna go change it now

1 Like

Fixed and working. It’s always the smallest things. Thanks!

1 Like

Yup, sometimes you just need a second pair of eyes. Good work getting it working!

Yup so true! Thanks agin for the help.
BTW organic compiler is an awesome tag!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.