Hello Fellow Campers!
Can anyone tell me why this solution to the “Smallest Common multiple” algorithm doesn’t pass? it is tested and working in chrome when I console.log test it.
I suspect it is a runtime thing for the last case?
Anyway here is the code :
function smallestCommons(arr) {
let newArr = arr.sort((a, b) => {return a - b});
let n = newArr[newArr.length - 1];
let N = [];
for (var i = newArr[0]; i <= n; i++) {
N.push(i);
};
for (var i = 2; i < 10000000; i++) {
let a = N.every((item) => {
return i % item === 0
});
if (a === false) {continue}
else if (a === true) {console.log(i, "found"); return i}
}
}
smallestCommons([23, 18]);