Smallest Common Multiple help2

Hi guys I can’t seem to spot any errors in my code to complete this challenge, I hope you guys can lend me your eyeballs for this :eyes:
link to challenge: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/

//noprotect
function smallestCommons(arr) {

 let maxNumber =  Math.max(arr[0],arr[1]);

 let minNumber =  Math.min(arr[0],arr[1]);
 

let LCM = 0;

let flag = true;

while (flag) {

LCM++;

for (let i = minNumber; i <=maxNumber; i++) {

if (LCM % i !== 0) {
  break;
}

else if (i===maxNumber) {
  flag = false;
}

}

}

return LCM;
}


console.log(smallestCommons([1,5]));

What tests are that you don’t pass?

smallestCommons([1, 13]) should return 360360.

smallestCommons([23, 18]) should return 6056820.

It seems your code is triggering the infinite loop protection because it is taking too much time to execute. You will need to refactor your code and and make it more efficient

1 Like

i changed “LCM++” to “LCM+=20” and it worked thanks :slight_smile:

kinda odd if i click on “run test” sometimes it works, sometimes it doesn’t… and the answer is different from the supposed answer on the requirements.

//noprotect
function smallestCommons(arr) {

 let maxNumber =  Math.max(arr[0],arr[1]);

 let minNumber =  Math.min(arr[0],arr[1]);
 

let LCM = 0;

let flag = true;

while (flag) {

LCM+=20    

for (let i = minNumber; i <=maxNumber; i++) {

if (LCM % i !== 0) {
  break;
}

else if (i===maxNumber) {
  flag = false;
}

}

}

return LCM;
}


console.log(smallestCommons([23, 18]));

could u test this to see for urself? like this is so odd

in this case it will eventually work because you are checking only multiples of 20, which is a number in the range [23, 18], but if there is not, then you will get the wrong result…

see here the example: https://repl.it/@Ieahleen/replformultyplayer (it’s your code, just tidied up a bit, if you press “run” you will see in the console what the code is doing, thanks to a bit of liberal use of console.log statementes) , it gives 60 if you pass in [1,3], it should just give 6

so, your code is more efficient for [23, 18] but it can’t work for many other things