# Smallest Common Multiple wont pass with correct answer

Tell us what’s happening:
My code works but it will not pass. you can copy it into Chrome Dev tools console and get all the correct answers, but it won’t pass - How come?

``````
function smallestCommons(arr) {
var sortedArr = arr.sort(function(a,b) {
return a-b;
});
var range = [];
var num = sortedArr;
for(var i = sortedArr; i <= sortedArr;i++) {
range.push(i);
}
while(range.length != (range.filter(cur => num % cur === 0)).length) {
num++;
}
console.log(num);
return num;
}
smallestCommons([1, 5]);
smallestCommons([2, 10]);
smallestCommons([1, 13]);
smallestCommons([23, 18]);
``````

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

FreeCodeCamp has an infinite loop protection that monitor how much time a loop is executing and if it takes too much the loop is stopped

Seeing that with your loop you are going up one number at a time, if the number to reach is high it will need a lot of steps and time to reach it, it is highly probable that this is what happening

arrrgh!! How do you refactor code for speed? Sheesh…they haven’t taught that yet…

You need to find a different method that doesn’t require you to check each number up to several thousands

Probably you would need to change approach, you can search for an algorithm that let you find the smallest common multiple and try to implement that

Try Euclid’s algorithm to find the GCD, and from there you can compute the LCM.

I might try that, I was thinking of making them decimals and tryng to find the LCD, but I got lost from there. So I was looking into finding the Least common Prime Factor of each and multiplying them together to give me their Smallest Common Multiple.

Thank you for the direction Chuck!

Tried very hard to understand your solutions, but I just could not wrap my head around it. My solution is as follows and beats the hell out of me why the code in the comments is much shorter and prettier but takes longer to run and times out on the last 2 tests, but the code that follows is much longer, not so pretty, and runs much quicker… that’s just strange to me:

``````function smallestCommons(arr) {
var sortedArr = arr.sort(function(a,b) {
return a-b;
});
var range = [];
for(var i = sortedArr; i <= sortedArr;i++) {
range.push(i);
}

// code below works but takes too long - times out on last 2 tests

// var num = (sortedArr * sortedArr) * range.length;
// while(range.length != (range.filter(cur => num % cur === 0)).length) {
//   num++;
// }

// console.log(range);
// // console.log(num)
// return num;

// While code is longer and not so pretty, is runs faster
var pnums = [2,3,5,7,11,13,17,19,23];
var pArr = [];
for(var i = sortedArr; i <= sortedArr;i++) {
range.push(i);
}

for(var k = 0; k< pnums.length;k++) {
while (range.filter(cur => cur %pnums[k] === 0).length !== 0) {
range = range.map(cur => {
if(cur %pnums[k] === 0) {
return cur/pnums[k];
} else {
return cur;
}
})
pArr.push(pnums[k]);
}
}

const reducer = (accumulator, currentValue) => accumulator * currentValue;
console.log(pArr.reduce(reducer));
return(pArr.reduce(reducer));
// console.log(num)
//  return num;
}
smallestCommons([1, 5]);
smallestCommons([5, 1]);
smallestCommons([2, 10]);
smallestCommons([1, 13]);
smallestCommons([23, 18]);
``````