What to do to deal with large input - Smallest multiple

I am solving Smallest multiple challenge on freeCodeCamp’s curriculum.
I pass all tests except one test.

Screen Shot 2022-01-20 at 7.51.22 AM

   **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

Challenge: Problem 5: Smallest multiple

Link to the challenge:

What do you get when you run smallestMult(20)?

If it works for every other test case, I’d be suspicious of how long it takes your code to run.

Test failed because Potential infinite loop detected

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.

I don’t understand you, Do you mean include GCD beside the for loop ?

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.

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