Smallest Common Multiple: Brute-force solution?

Smallest Common Multiple: Brute-force solution?
0

#1

Tell us what’s happening:

Hey guys, using a brute force solution here but not passing the last test. It appears that the number returned is different every time whenever I run it on JSbin. Does anyone know why?

Your code so far


function smallestCommons(arr) {
  const MIN = Math.min(...arr);
  const MAX = Math.max(...arr);
  let isFound = false;
  let currNumber = 0;
  
  while(!isFound) {
    currNumber += MAX;

    for(let i=MAX; i>= MIN; i--) {
      if(currNumber % i !== 0) {
        break;
      }
      else if(i===MIN) {
        isFound = true;
      }
    }
  }

  console.log(MIN + " " + MAX + " " + currNumber);
  return currNumber;
}


smallestCommons([23,18]);

Your browser information:

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

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


#2

yup, you’re hitting the FCC timeout

If you search the forum you can find others who did the same and suggestions on workarounds and code-rewrites you can do.