Smallest Common Multiple - potential infinite loop on some test cases

Smallest Common Multiple - potential infinite loop on some test cases
0

#1

Tell us what’s happening:
my function passes all the tests except the last two, which happen to contain much larger integers resulting in larger loops. The error that i’m getting when i run the function with these arguments - [1, 13] or [23, 18] in repl.it is that there is a potential infinite loop? Does anyone know why this is happening, and could it be that the test suites cap loops to a maximum iterations before they’ll throw a potential infinite loop warning, even if eventually the function would return and the loop would stop?

Your code so far


function smallestCommons(arr) {
  let sorted = arr.sort((a, b) => a-b)
  let s = sorted[0]
  let b = sorted[1]
  let target = (b-s)+1
  for(let c = b*2; c; c++){
    let count = 0
    for(let i = s; i <= b; i++){
      if(!(c%i)){
        count++
      }
      if(count == target){
        return c
      }
    }
  }
}


console.log(smallestCommons([1,5]));

Your browser information:

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

UPDATE: I just disabled the infinite loop protection on repl.it and my function returned the target values for the all the tests on FCC.
This leads to a new question of how do i disable infinite loop protection on FCC so i can get credit for passing the challenge?


#3

You can not. You must refactor your solution to use a more efficient algorithm.


#4

so far c+=b*2 passed [1, 13]. just gotta refactor some more


#5

passed! Thanks again!