I am trying FCC Intermediate Algorithm Scripting: Smallest Common Multiple and have come up with my own code. It works fine with all the test but [23, 18]. Any idea why the following code does not return the correct result for [23, 18]. https://jsconsole.com/ and https://js.do/ sites run the code fine and output the correct results. I have come across some similar issues on FCC challenges.
function smallestCommons(arr) {
let scm = 0
let arr2 = [Math.min(...arr)];
for(let i = Math.min(...arr)+1; i <= Math.max(...arr); i++) {
arr2.push(i);
}
for (let k = 1; k <= arr2.reduce((x,y) => x*y); k++) {
if(arr2.every((n)=>{return (k % n === 0); })) {
scm = k;
break;
}
}
return scm
}
because it seems that the other editor you are using doesn’t have an infinite loop protection, or the code can run for more time before being stopped
Tried all these - not working again… I don’t quite get the infinite part… this loop has to run 6056820 times to return the result which is 6056820. the product of all number in the array [18, 19, 20, 21, 22, 23] is 72681840 which is 12 times bigger than 6056820. Well I understand the time part but it eventually should finish as it is NOT a loop that runs forever.