Tell us what’s happening:
I’m trying to work on the Smallest Common Multiple challenge, and I’m so far hating the complexity and lack of practicality of it. I’m using a recursive function for this, and running my function with “big” (like 2 digit) numbers will create an infinite loop that freezes my browser.
console.log(JSON.stringify(
smallestCommons([1, 5])
))
Would return 60 without problem, but doesn’t pass the test cases, because
console.log(JSON.stringify(
smallestCommons([2, 10])
))
is holding it up with an infinite loop. Not fun.
Your code so far
function smallestCommons(arr) {
const allBetweenArray =
[...Array(Math.max(...arr) - Math.min(...arr) + 1)]
.map((value, index) => Math.min(...arr) + index);
const lcm = function leastCommonMultipleGivenArray(arr, initialLength) {
return arr.find(
(num, index, arrLevel2) =>
arrLevel2.filter((numLevel2) => num === numLevel2)
.length === initialLength
) || lcm(
[
...arr,
...arr.slice(-1 * initialLength).map((num, index) => num + arr[index])
],
initialLength
);
}
return lcm(allBetweenArray.sort((a,b) => b- a), allBetweenArray.length);
}
console.log(JSON.stringify(
smallestCommons([1, 5])
))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/