Smallest Common Multiple Exercise - SPOILER - MY SOLUTION

Smallest Common Multiple Exercise - SPOILER - MY SOLUTION
0.0 0


I spent probably 5 times more time on this exercise than I have on any other non design related one - and partly because I spent a lot of time trying to do a series of prime factorials by looping through things and it turns out that isn’t that way. But since it’s the kind of thing I wouldn’t be able to sleep until I finished (cause it’s 12:17 here and I’ll be up at 5:30) i kind of had to keep pushing at it.

Many unhelpful people on the chat were discouraging but one person reminded me of how I could use objects, and while this may be ‘smelly and dirty’ at least it’s mine.

function smallestCommons(arr) {
  arr = arr.sort(function(a,b){
    return a-b;
  var testArray = [];
  for (i=arr[0]; i<= arr[arr.length-1]; i++) {
    if (i !== 1) {
  var result = 1;
  var primes = [];
  for(i=2; i<= arr[arr.length-1]; i++) {
    var count = 0;
    if (i<4) {
    } else {
      for(j=0; j<primes.length; j++) {
        if (i%primes[j] === 0) {
          count = 1;
      } if (count === 0) {
  var obj = {};
  for (i=0; i<testArray.length; i++) {
    var x = testArray[i];
    for(j=0; j<primes.length; j++) {
      var y=0;
      var z = primes[j];
      if (!obj.hasOwnProperty(z)) {
        obj[z] = 0;
      while(x%primes[j] === 0) {
        y ++;
        x/= primes[j];
      if (obj[primes[j]] < y ) { obj[primes[j]] = y; }
      if (x == 1) { continue; }  
  var test = Object.keys(obj);
  for (i=0; i<test.length; i++) {
    if (obj[test[i]] > 0) {
      var c = 1;
      while(c <= obj[test[i]]) {
        result *= parseInt(test[i]);
  return result;




Actually when i posted my ooriginal solution (which attempted to do prime factorial on each number individually and then add it to the result AFTER removing the prime numbers - which seemed to make sense at the time - was called dirty - it took me three hours after i first thought of ‘object’ to get to it.

I read the FCC answer but I didn’t like it because it didn’t seem like the ‘math way’ to do it - and I read a bunch of the GCD stuff but still couldn’t figure out the coding like others did.

Issues like this make me realize how much I really wanna code - but also - are just frustrating cause I knew I wouldn’t sleep until I had it solved (my way, not the FCC way just to get past it :wink:


I was referring to the ‘wiki’ results they post (and post in here and lock the commnts - you can find it if you just search greatest common multiplier)


Well - while it 's been a while - i was pretty good at math through calc AB so i did some note taking and worked out the ‘mathy bits’ on paper using prime factoring and then ‘crossing out’ the duplicated numbers - it just took a long time to figure it out properly.

I have no idea if this works on any other ranges - but i know it works to pass the test

And - if there’s ever a ‘factor this number’ exercise - which i think there is - i htink i solved it too :slight_smile:


you could rewrite the gcd as a while loop - i so that last night.

Recursion has always been tricky for me - plus I still dont’ fully understand the MATH behind the GCD algorithm - i didn’t really look at it closeles - and I’d have to get the math first before anything else.

To me instinctively it said ‘this is a job for prime numbers’ so that’s where i started (and honestly as it came AFTER the prime number exercise I thought that was on purpose) - so the first step was to rebuild my prime number array up to the max number.


Ive approached this by creating an array of numbers to multiply them with parameters-array to get array of multiplies at the end. It worked with smaller numbers (in range up to 1000) but to get 360360 and millions one I need to set i to 360360 at least, and this creates a huge tax on browser.

Is there any way to reduce bloat while staying in frame of this approach?