Loop breaks due to JS Bin timing restrictions

Loop breaks due to JS Bin timing restrictions
0

#1

Tell us what’s happening:

On the Smallest Common Multiple challange I use a while-loop solution that executes one time for a given number (num), where it checks the modulo return over each item using a filter. This works just fine for smaller ranges (e.g. [2,10],) but the in-built protection implemented by FCC using JSBin stops the loop before it finishes, and the last two challanges fail as a result of this. I have tried to add //noprotect at several places in the code, with no results. Below is my latest try. (I have also tried to add //noprotect above and within the function.)

Your code so far


/*jshint esnext: true */

function smallestCommons(arr) {

  arr.sort((a,b) => a-b);
  let directive = [];
  for (let i = arr[0]; i<=arr[arr.length-1];i++) {
    directive.push(i);
  }
  console.log(directive);


  let num = 1;
  let numFound = false;

  while(numFound == false) {

    let filtered = directive.filter( item => num % item == 0);

    filtered.length == directive.length ? numFound = true : num++;
  }
  console.log(num);
  
  return num;
}

//noprotect

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.81 Safari/537.36.

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


#2

Your algorithm is just not efficient enough to not set off the test suite’s infinite loop protection. In the new curriculum, no protect does not work, so your only option is to rethink how you could use a more efficient algorithm to solve the problem.