Here is my code:
function smallestCommons(arr) {
let minNum = Math.min(...arr);
let maxNum = Math.max(...arr);
//fill out array
for (let i = minNum+1; i<maxNum; i++) {
arr.push(i);
}
let maxMultiple = arr.reduce((acc,next) => acc*next);
for (let j = maxNum; j < maxMultiple; j+=maxNum) {
if (arr.every(x => j % x === 0)) { return j;}
}
return maxMultiple;
}
smallestCommons([1,5]);
I am not passing the last test. Last time I went through the material, I remember having this problem. I cannot remember, though, what I did to avoid it (other than increment by maxNum rather than simply the incremental operator.
How can I fix this to make it efficient enough to pass the last test?