Smallest Common Multiple: final test

Smallest Common Multiple: final test
0.0 0

#1

Tell us what’s happening:
I think it true but can not passing final test.
help me, please.

Your code so far


       function smallestCommons(arr) {
        const check= (s , min, max)=>{
          for(let i=min; i<= max; i++){
              if(s %i !== 0){
                return false;
              }
            }
            return true;
        }
          if(arr[0]== arr[1]) return arr[0];
          let max= Math.max(arr[0], arr[1]);
          let min= Math.min(arr[0], arr[1]);
          let s= max;
          while(!check(s, min, max)){
                s+=max;
            }
            return s;
        }

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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


#2

It passes for me. Try rebooting, trying it in a different browser, clearing the browser cache, etc.


#3

The same thing is happening to me with my code. I know it’s right because it returns the correct answer in my browser consoles and in Code with Quokka. I’ve tried in Chrome and FF and it still fails the last test. My code:

function smallestCommons(arr) {
  const a = Math.min(...arr);
  const b = Math.max(...arr);

  if (a === b) return a;

  const range = Array.from({ length: b - a }, (val, index) => {
    return a + index;
  }).slice(1);
  let i = 1;
  let mult;

  while (mult === undefined) {
    if (i % a === 0 && i % b === 0) {
      if (range.every(num => i % num === 0)) {
        mult = i;
      }
    }
    i++;
  }

  return mult;
}

smallestCommons([1, 5]);


#4

Hi,

Both the Smallest Common Multiple and the Sum All Primes challenge are likely not pass all tests unless you are careful to craft an efficient algorithm for your solutions. Excessive looping or excessive amount of execution time will convince the FCC environment that you have an infinite loop condition and so stop execution.

@mewmew Your while loop cycles LCM times. Or if your LCM=60 that loop executes 60 times - not a big deal. But if your LCM is 6056820 , then that loop also executes a whopping 6056820 times.

@ndnhan132 For the same test [23,18] your while loop cycles 263339 times.

Both of your solutions “work” in that they get the right answer but they take too long (or too many steps) to get that answer.

Because this is such a difficult challenge there are lots of posts in the forum about this very problem. You might want to search “Smallest Common Multiple” to see if there are any helpful hints in past posts.

Good luck


#5

Thank you! I fixed it. Wrote it in the middle of the night and realized the now-obvious-to-me error making it run LCM times. Thank you!


#6

// no-protect

I think is a way to prevent infinite loop error w/ FCC.


#7

Thanks @John-freeCodeCamp. I tested // no-protect, but it did nothing.

Like @mewmew and @ndnhan132, I too wrote a quick solution that looped LCM times because my goal is just to knock out the challenges as fast as possible. Performance wasn’t an issue. And I certainly wasn’t interested in learning math.

I ended up spending half a day on increasing performance, and I didn’t have to learn math :slight_smile:
Since I wasted all that time, I have to post my rather concise accomplishment :stuck_out_tongue:

function smallestCommons(arr) {
  arr.sort((a,b) => a-b);
  const range = [...Array(arr[1]-arr[0]+1).keys()].map(x => x+arr[0]).reverse();
  let scm=0, match=[false], performance=0;

  while (match.includes(false)){ 
    performance++;
    scm += range[0]*range[1];
    match = range.map(x => scm % x == 0);
  }

  console.log(arr,range);
  console.log("Loops: "+performance, "SCM: "+scm);
  return scm;
}

smallestCommons([1,5]);

My results, only 11970 loops on the last test.

VM16594:30 (2) [1, 5] (5) [5, 4, 3, 2, 1]
VM16594:31 Loops: 3 SCM: 60
VM16594:30 (2) [2, 10] (9) [10, 9, 8, 7, 6, 5, 4, 3, 2]
VM16594:31 Loops: 28 SCM: 2520
VM16594:30 (2) [1, 13] (13) [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
VM16594:31 Loops: 2310 SCM: 360360
VM16594:30 (2) [18, 23] (6) [23, 22, 21, 20, 19, 18]
VM16594:31 Loops: 11970 SCM: 6056820

#8

I didn’t know how to do it, so i looked up a youtube tutorial when i wrote mine

–> https://www.youtube.com/watch?v=LpGgtkmRvqc
–> https://repl.it/@John_Nicole/Intermediate-Algorithm-Scripting-Smallest-Common-Multiple