# Smallest Common Multiple (Loop issue)

Tell us what’s happening:
Hello all. I have figured out a solution to this problem but with limitation. My `while` loop needs to be set to a high number IOT process larger inputs. I currently have it set to 10000 to pass the challenge but I’m sure there’s a way to make it stop once the condition is met .

I have also come up with this…

``````while (commonCounter !== sortedArr.forEach(x => {
if(commonCounter % x !== 0) {
commonCounter = commonCounter + commonMultiple;
}}));
``````

…and although it works as expected, I get a potential loop error and then after about 10 seconds it passes the challenge.

Any suggestions on a better way to implement a loop here?

``````  **Your code so far**
``````
``````  [spoiler]
function smallestCommons(arr) {
//sorting numerically
let sortedArr = [];
let sorted = arr.sort((a,b) => a - b);
//loop used for pushing numbers between sorted[0] and sorted[1]
for (let i=sorted[0];i<=sorted[1];i++) {
sortedArr.push(i);
}
console.log(sortedArr)

//create a common multiple by using sorted[0] times sorted[1]
let commonMultiple = sorted[0] * sorted[1];
let commonCounter = commonMultiple;
let r = 0;
while (r < 10000) {
sortedArr.forEach(x => {
if(commonCounter % x !== 0) {
//continue adding commonMultiple until all elements (x) evenly divide
commonCounter = commonCounter + commonMultiple;
}});
r++
};
console.log(commonCounter)
return commonCounter;
}

smallestCommons([13,1]);
[/spoiler]
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63`.

Challenge: Smallest Common Multiple

Choosing an arbitrary number of times to loop may work to satisfy the tests, but it doesn’t reallly accomplish the task well. For some inputs, it will make the function run much longer than it needs to. For other inputs, it might not run long enough.

If I recall correctly, the wikipedia page for Least Common Multiple is a good resource for the formulae that you can use to solve this efficiently. You’re not required to reinvent mathematical techniques. (LCM is not exactly the same as SCM, so you’ll need to do additional work to solve the problem.) There are other good resources out there for helping with the math of this challenge - it all just depends on what style of explanation works best for you.

2 Likes

Yeah I tried using another method for the loop as stated above. It works but has some delay to it.

I though about finding the LCM of the two args first but decided to just multiply them since all numbers in between need to be able to divide into the answer which means the answer will be higher than (and a multiple) of this number.

From that point it was simply running a loop until the condition of each number was satisfied.

I’ll look at more loop options. Thanks.

Loop based solutions will be slow and often trigger the infinite loop warning.

The LCM and GCD articles on Wikipedia provide a much better algorithm.

1 Like

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