Smallest Common Multiple not passing last two tests

Tell us what’s happening:
Hi Everyone,

My code passes for Smallest Common Multiple except for the last two tests. I ran my code at repl.it and the code passes both those cases there so I’m not sure why it isn’t passing on FCC. Can anyone help? I’ve reset the code a couple times and it still isn’t passing.

Your code so far
function smallestCommons(arr) {
arr.sort(function(a, b) {return a - b});
let least = arr[0];
let greatest = arr[1];
let numsBetween = [];

for (let b = least; b <= greatest; b++) {
numsBetween.push(b);
}

for (let i = least; true; i++) {
if (i % least === 0 && i % greatest === 0) {
if (numsBetween.every(ele => i % ele === 0)) {
return i;
}
}
}
}


function smallestCommons(arr) {
  arr.sort(function(a, b) {return a - b});
  let least = arr[0];
  let greatest = arr[1];
  let numsBetween = [];

  for (let b = least; b <= greatest; b++) {
    numsBetween.push(b);
  }

  for (let i = least; true; i++) {
    if (i % least === 0 && i % greatest === 0) {
      if (numsBetween.every(ele => i % ele === 0)) {
        return i;
      }
    }
  }
}


smallestCommons([1,5]);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple

You are probably triggering the infinite loop protection. You check the numbers one by one, but that would mean that your code for the last test would need thousands of steps.

You need to find a way that needs less resources.

3 Likes

This makes sense! I will go back and try to figure out a different way. Thank you.