Smallest Common Multiple, can't pass the last two tests

Smallest Common Multiple, can't pass the last two tests
0

#1

Tell us what’s happening:

I’m not passing the last two tests. I don’t know if it has something to do with larger values.

Your code so far


function smallestCommons(arr) {
  var small = Math.min(...arr);
  var large = Math.max(...arr);
  var array = [];
  for(var i = small; i <= large; i++) {
    array.push(i);
  }

  var x = true;
  var LCM = 0;
  while(x) {
    LCM++;
    for(var j = array[0]; j <= array[array.length - 1]; j++) {
      if(LCM % j !== 0) {
        break;
      }
      else if(j == array[array.length - 1]) {
        x = false;
      }
    }
  }
  console.log(LCM);
  return LCM;
}










smallestCommons([1,5]);











Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

Your code appears to give the correct output for the failing tests - I’m not sure on this, but there may be something behind the scenes that stops your code from running through the tests if there’s too many calculations involved or something - for preventing infinite loops, and crashing your browser. Your code doesn’t have an infinite loop, but there’s probably a lot of calculations in those last two tests that may be triggering the testing to be stopped. Don’t quote me on this, I’m trying to find a post or something to confirm…


#3

https://forum.freecodecamp.org/t/free-code-camp-infinite-loop-protection/19550
this post explains the problem, and gives a solution - however, I don’t think that solution works at the moment on the new website - I don’t know of any other quick solutions, you could try and make your algorithm faster to finish the tests within the time limit


#4

Usually when you fail the test for the large numbers but it works elsewhere (like it does in codepen) that means that it is timing out before you can get the answer. The testing has a built in time limit. This means that you have to make your code more efficient. This is a common issue in programming. Try to find ways to maker your algorithm more efficient.