Smallest Common Multiple console.log issue

Smallest Common Multiple console.log issue
0

#1

Tell us what’s happening:
Hi! This is my first time posting to the forums, so please forgive me for any transgressions; particularly the sloppy code/commenting.

I had two questions regarding this challenge:

A: When I comment out the console.log’s I’m using to debug at 19, 23, and 27, for some reason, some of the fulfilled stories are no longer fulfilled. For instance, if the console.log at 27 is commented out, I fulfill the first 4 requirements of arguments passed through my function, but when that line isn’t commented out, I only pass the very first set of arguments. Shouldn’t the console.log commands not affect the rest of the code?

B: My code is able to fulfill the first 4 sets of arguments, but I can’t figure out why the last two sets aren’t going through. When I attempt to debug via console.log to see what’s going on; it seems my ‘j’ in the main for loop isn’t going all the way to its limiter that I listed. Any ideas?

Again, this is my first forum post, so I apologize if I’ve done anything redundant or out of turn. Thank you in advance, this site has been absolutely stellar so far for a brand new coder!

Your code so far


function smallestCommons(arr) {
  let args = arr.sort((a,b) => a - b);
  let argSpread = [], argsArrangeSpread = [];
  let argMin = args[0] * args[1];
  let finalNum = 0;
  var numCount;
  for (let i = args[0]; i <= args[1]; i++) {
    argSpread.push(i);
  }  
  var argMax = argSpread.reduce((a,b) => a*b);
  argsArrangeSpread = argSpread.sort((a,b) => b-a);
  let spreadLength = argsArrangeSpread.length;
  for (let j = 1; j < argMax; j++) {
    numCount = 1;
    if (finalNum == 0) {
    //console.log(numCount, j, argMax, spreadLength);
    for (let k = 0; k <= spreadLength; k++) {
      if (j % argsArrangeSpread[k] == 0) {
        //console.log(numCount, j, spreadLength, arr, argMax, "this one works");
        numCount++;
        continue;
      } else if (numCount == spreadLength + 1) {
       // console.log("this time", j);
        finalNum = j;
        break;
      } else {
       //console.log(numCount, j, argsArrangeSpread.length, "next", arr, argMax)
        break;
        }
      }
    } else {
      break;
    }
  }
  //console.log(finalNum, arr);
  return finalNum;
}


smallestCommons([1,5]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; 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

The last two are too expensive to compute for your current solution

FCC has a loop protection to (hopefully) protect you from locking up your browser on an infinite loop, but it’s not mega sophisticated

It essentially gives you about 100ms for a whole loop to complete

This is usually the first question people come across this, and it can be disabled if you want to, but I’d strongly recommend coming up with a more efficient solution to the problem

In particular here’s a couple of starting points I think will help a lot

Can you write the lowest common multiple of a set in numbers in terms of a function that can find the lowest common multiple of two numbers? Think why this has to be

There’s an easy way to calculate the LCM of two numbers in terms of the greatest common denominator

There’s a famous algorithm for finding the greatest common denominator, worth looking up and trying to implement yourself imo, but I’m sure whatever you come up with will be good enough

Hopefully these are enough of a starting point to get you going. The first two points of the three there are the most important


#3

Gotcha, thank you for the info; I’ll work on it!


#4

Holy crap, that made it a lot easier than how I was trying to do it. Thank you so much!
Solution I ended up on:

function smallestCommons(arr) {
  let args = arr.sort((a,b) => a - b);
  var argSpread = []
  for (let i = args[0]; i <= args[1]; i++) {
    argSpread.push(i);
  }
  
  //gcd recursive
  let gcd = function(a,b) {
    if (b) {
      return gcd(b, a%b);
    } else {
      return a;
    }
  }
  let lcm = function (a,b) {
    return (a*b)/gcd(a,b);
  }
  //answer starts as minimum range
  var answer = argSpread[0];
 argSpread.forEach(function(x) {
    answer = lcm(answer, x);
  })
  console.log(answer);
  return answer;
}


smallestCommons([1,5]);

#5

Gratz!

You accidentally reinvented reduce at the end, but that’s pretty much what I was hoping you’d do