# Intermediate algorithm challenge - smallest commons

Hey guys,

I’m working on completing the smallest commons algorithm challenge. My code seems to have the correct output locally (through Atom), but fails the two following tests on FCC:

smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.

Here’s my code:

``````function smallestCommons(arr) {
arr.sort( (a, b) => { // puts values in order
return a - b;
});
let newArray = []; // new array of each integer in between
for (let i = arr; i <= arr; i++) {
newArray.push(i);
}
let max = newArray.reduce( (acc, value) => { // the largest possible lcm
return acc * value;
});
for (let i = newArray[newArray.length - 1]; i <= max; i++) { // iterates through each possible lcm
let divCheck = newArray.every( (value) => { // returns true if number is divisble by every value in the array
return i % value == 0;
});
if (divCheck == true) { // returns the truthy value
return i;
}
}
}
``````

Any suggestions or advice on how to pass the test would be great and, more importantly, any tips on how to write better code. Thanks!

Your solution is not efficient enough to pass the tests in the time expected. All the FCC challenges can be solved with an efficient algorithm. When looking at your code, it has many parts which iterate through arrays. The nested every method within the 2nd for loop is probably taking the longest for these two particular tests.

That makes sense. It does take some time to run these two:

smallestCommons([1, 13])); // 0.36 seconds
smallestCommons([23, 18]); // 0.838 seconds

Do you know what the run-time parameters are? Just for future reference. Thanks!

I vaguely recall there being some kind of message if infinite loop detection happens from taking too long, is that no longer the case?