I am solving Smallest multiple challenge on freeCodeCamp’s curriculum.
I pass all tests except one test.
**The code so far**
function smallestMult(n) {
let newArr = [];
let largestPossibleValueForSCM = 1;
for (let i = 1; i <= n; i++) {
newArr.push(i)
largestPossibleValueForSCM *= i;
}
for (let i = n + 1; i <= largestPossibleValueForSCM; i++) {
if (newArr.every((number) => i % number === 0)) {
return i
}
}
return ;
}
smallestMult(5);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0
Ahhh, your code is too slow. I would research a faster algorithm. You can do the small fix or the ‘right’ fix here. In the first, you just make your loop iterate by bigger steps. In the second you look at the GCD.
Either you need to make the loop take bigger steps OR you need to replace the loop with the GCD based algorithm.
In my opinion, it’s worthwhile looking at the LCM and GCD on Wikipedia and trying to implement the algorithm yourself. It’s good practice for actual developer work.