Smallest Common multiple solution doesn't pass; but it works in testing

Smallest Common multiple solution doesn't pass; but it works in testing
0.0 0

#1

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]);



#2

Yep, probably. Your function terminates early somewhere in the middle of the last test case due to too many iterations.

I think there are more efficient solutions to this exercise. Perhaps push yourself to find one?