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