Smallest Common Multiple doesn't work

Hey everyone.
I have a big problem, my code works on google console window but it doesn’t work on FCC text editor, i don’t know why ?
Thank Clément.


function smallestCommons(arr) {
  var min = Math.min(...arr);
  var max = Math.max(...arr);
  var all = [], i = 1;

while (min <= max){
  all.push(min);
  min++;
}
while (!all.every(x => i%x == 0)){
  i++
}
return i;
}

smallestCommons([23,18]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:

It is probably not enough efficient and it is trigggering the infinite loop protection because it is taking too much time to execute

How I can fix the problem ?

You need to refactor your code so that it needs less steps. You are checking one by one numbers from 1 to several thousands… it is a bit much

I found the solution !!! :smiley:

function smallestCommons(arr) {
  var min = Math.min(...arr);
  var max = Math.max(...arr);
  var all = [], i = max;

while (min <= max){
  all.push(min);
  min++;
}
while (!all.every(x => i%x == 0)){
  i += max;
}
console.log(i);
return i;

}

smallestCommons([23,18]);
2 Likes