Tell us what’s happening:
Please see smallest common multiple challenge.
My code passes all tests besides the final, which uses large numbers. I am guesing it doesnt pass because it is not efficient enough to process the large numbers.
Any help on how I could change this code to make it more efficient or otherwise finish this challenge would be much appreciated. Thanks.
Your code so far
function smallestCommons(arr) {
let a = arr.sort((a,b) => a-b)
let b = [...Array(a[1]+1).keys()]
let c = b.slice(b.indexOf(a[0]))
// c is array of all numbers to be tested. a and b are just stepping stones to c
let d = []
// empty array that will end up holding every common multiple smaller than fin
let fin = c.reduce((a,b) => a*b)
// all numbers in c multiplied together
let least = function(nums, f) {
for (let i of nums) {
if (nums.every((j) => (f/i)%j == 0)) {
d.push(f/i)
if (Math.min(...d) != f) {
let f = Math.min(...d)
if (nums.every((j) => f%j == 0)){ return least(nums, f) }
}
}
}
return d[d.length-1] // once a value is reached that is not a common multiple, return the value before
}
return least(c, fin)
}
console.log(smallestCommons([2,10]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/