Help: Intermediate Algorithm Scripting: Smallest Common Multiple

Help: Intermediate Algorithm Scripting: Smallest Common Multiple
0

#1

Hi all,

This challenge seems to work on my node REPL but on FCC the last check won’t pass.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple

Any ideas?

Thanks in advanced for any advice.

function smallestCommons(arr) {
  let found = false;
  let counter = 2;
  arr.sort((a,b) => (a - b));
  while (found == false){
      for(let i = arr[0]; i <= arr[1]; i++){
          if(counter % i != 0){
              counter++;
              break;
            }
            if(i == arr[1]){
                found = true;
            }
        } 
    }
    return counter;
}


console.log(smallestCommons([23,18]));

#2

i believe you are running into the FCC ‘infinite loop’ prevention because it takes too long to complete. If you search the forum you can find other posts similar to yours and see how others handled the same situation. (there is a workaround you may try or you can rewrite the code to be more efficient)


#3

Described in this link: Intermediate Algorithm Scripting: Smallest Common Multiple - can't pass it