How Have You Grown as a Coder?

Helping people with the algorithms, I end up going back and re-factoring my own solutions. This is good practice, but on the last one, I was amazed at the difference between my solution to Smallest Common Multiple today, versus the solution I came up with last year:

Last Year:

function smallestCommons(arr) {
    var baseNumbers = [];
    arr.sort(function (a, b) { return a - b; });
    for (var i = arr[0]; i <= arr[1]; i++) {
        baseNumbers.push(i);
    }
    var divisible = true,
        divisorList = [],
        currentDivisor = 2;
    while (divisible) {
        divisible = false;
        for (i = 0; i < baseNumbers.length; i++) {
            if (baseNumbers[i] % currentDivisor === 0) {
                baseNumbers[i] = baseNumbers[i] / currentDivisor;
                divisible = true;
            }
        }
        for (i = 0, baseValues = 0; i < baseNumbers.length; i++) {
            baseValues += baseNumbers[i];
        }
        if (divisible) {
            divisorList.push(currentDivisor);
        }
        else if (baseValues !== baseNumbers.length) {
            currentDivisor++;
            divisible = true;
        }
    }
    var LCM = 1;
    for (i = 0; i < divisorList.length; i++) {
        LCM *= divisorList[i];
    }
    return LCM;
}

Today:

function smallestCommons(arr) {
  arr.sort();
  var LCM, mult = 1, check = false;
  
  while(!check){
    mult++; check = true; LCM = arr[1] * mult;
    for(var i = arr[0]; i < arr[1]; i++) if(LCM % i) check = false;
  }  
  return LCM;
}

I’m not saying that the new solution is the best one, and as I gain more experience I may think of better solutions to this and all the algorithm challenges, but I still find it fascinating to see how much I’ve grown. Do you have any interesting examples of how you’ve grown as a coder?

1 Like

As a side note, whenever I start feeling really smart, I do some codewars challenges and that humbles me right down.

Very compact algorithm, but it will actually fail sometimes due to the way sort() works. Try [99, 100]. It should be 9900, but it will return ‘198’.

There is no function supplied to the sort function, so it will sort by converting the input to a string and then compare unicode vales. Since ‘1’ is less than ‘9’, 100 will come first. If you change it to this:
arr.sort(function compareNumbers(a, b) {
return a - b;
});
it will work as expected.

Also, the algorithm is pretty slow for larger numbers. I got a loop protect warning when running it on JS bin when trying [99,103]

1 Like

I admit that I know about the a-b trick, but I didn’t bother in this case since it wasn’t needed to pass the tests in the challenge. :smiley:

I’m only a month in but i’ve learned to match patterns with regexp on codewars and that allowed me to solve the phone problem much easier than the 50 if statements i thought i had to use!

I alternate between codewars and fcc now because the tougher algorithms are really tough and if i feel like i have no idea how to solve it, it’s usually because i’m missing a knowledge of something.

But sometimes it’s fun to solve things purely with if statements for for loops xD