Smallest Common Multiple (last two tests not passing)

Smallest Common Multiple (last two tests not passing)
0

#1

Tell us what’s happening:

For some reason the last two tests are not passing. If I test this code somewhere else I get the right values for the last two tests.

Your code so far


function smallestCommons(arr) {
  let arr = arr.sort((a,b) => a - b);
  let arr2 = [];
  
  for(let i = arr[0]; i <= arr[arr.length -1]; i++)
  {
    arr2.push(i);
  }
  
  let x = true;
  let LCM = 0;
  
  while(x){
    LCM++;
    for(let j=arr2[0]; j <= arr2[arr2.length -1]; j++)
    {
      if(LCM % j !== 0)
      {
        break;
      }
      else if(j == arr2[arr2.length -1]) {
        x = false;
      }
    }
  }
  
  

  return LCM;
}


smallestCommons([1,13]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0.

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


#2

This is a very common issue with that challenge. Your solution probably works but does not work efficiently enough so it sets off the infinite loop protection in free code camp

http://forum.freecodecamp.org/search?context=topic&context_id=224988&q=Smallest%20common%20last%20two&skip_context=true


#3

let arr = arr.sort((a,b) => a - b);

(arr is already declared as a parameter)

After that its the infinite loop protection error. You used to be able to disable it with //no-protect but now that doesn’t work.

let me guess, Dylan Israel?

He’s a nice FCC youtuber, i recommend you watch him.


#4

Thanks, yes I got this solution from Dylan’s youtube channel. I was stuck on this challenge. I try to figure out most of them on my own or use the hints, but some of them have been extra challenging for me. I have been on the Intermediate Algorithms for a few weeks now :(. Literally right now just before I wrote this response I am staring blankly at the Steamroller challenge, not really sure where to start…after a bit of time I will probably come up with something usually that half works or works for 2 or 3 of the tests.

As for this solution, I know this isn’t the most efficient solution because with large numbers it can run very slow. Although I have been programming for awhile (mostly self taught and from my college courses but not work related) and I have a CIS degree, I find algorithms and coming up with the most efficient solutions to be extremely challenging. In my course work in college I did not practice these kinds of Algorithms (I guess because CIS isn’t Computer Science although I did programming and web development) I went straight to actually making useful things, but I get that these are the kinds of “white board” questions you get in interviews so I want to get better at it, so that’s why I am here.


#5

I also used Dylan’s solution since i couldn’t figure it out either.

Practice make perfect. Don’t worry, i had a hard time also and then i slowly learned better and better ways.

We are glad to help here at FCC.


#6

Dylan Israel’s solution had half as many lines of codes as mine. If he created that video cold, I’m quite impressed.

I built an isPrime function, a greatest common denominator function and returned an array.reduce value.

Seven more hops until the projects.