Smallest Common Multiple[Help]

Smallest Common Multiple[Help]
0

#1

Tell us what’s happening:
This code works fine in other IDE but not in FCC, Why?

Your code so far


function smallestCommons(arr) {
  let num=1;
  while(true){
    let confirm=1;
    for(let start=Math.min(...arr),end=Math.max(...arr);start<=end;start++){
      if(num%start!=0){
        confirm=0; 
        break;
      }
    }
    if(confirm==1){
    // console.log(num)
      return num;
    }
    num++; 
  }
}

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

Your browser information:

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

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


#2

In what way does it not work? How is it behaving differently than you expect it to? What different things have you tried?


#3


This code works only when the maximum of the input range is below 12.

For example, function calls such as smallestCommons( [1,4]), smallestCommons( [3,10]), smallestCommons( [5,12]) gives expected results.

But smallestCommons( [1,13]), smallestCommons( [13,10]) doesn’t return any value at all.


#4

Sounds like you’re probably timing out. FCC uses infinite loop protection to keep your browser from crashing. It will stop your code from executing if it takes too long. Sometimes if your code is inefficient it will set off the ininite loop protection even if it’s not actually infinite.