Smallest Common Multiple - Works in browser console but not in FCC tests

Smallest Common Multiple - Works in browser console but not in FCC tests
0
#1

Tell us what’s happening:

When I run this in my browser’s JS console, it checks out. However, the FreeCodeCamp tests don’t seem to work. I think I’m losing my mind.

17%20PM

14%20PM

Your code so far


function checkNum(target,factors) {
  var everythingChecksOut = true;
  for (var i = 0; i < factors.length; i++) {
    everythingChecksOut = everythingChecksOut && target % factors[i] == 0;
  }
  return everythingChecksOut;
}
function getArrayRange(arr) {
  var min = Math.min(...arr);
  var max = Math.max(...arr);
  var newarr = [];
  for (var i = min; i <= max; i++) {
    newarr.push(i);
  }
  return newarr;
}
function smallestCommons(arr) {
  arr = getArrayRange(arr);
  var i = 0;
  var matchFound = false;
  while (!matchFound) {
    i++;
    matchFound = matchFound || checkNum(i,arr);
  }
  return i;
}


smallestCommons([23,18]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) 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/

0 Likes

#2

When it fails the last few tests (especially if it works elsewhere) it’s because your algorithm isn’t efficient enough. The FCC test has a timeout on it. See if you can find ways to make it more efficient.

1 Like