Post/Code

HomeAboutUsesNow

Mo' Coding Problems.

In my previous post, I said that "I feel that algorithms and mathematical-type programming is something I need to improve on". That was an understatement. I am terrible at them!

As evidenced by my struggle with the 2nd problem! (And these are the easy ones!)

Never has the term impostor syndrome felt so appropriate... :(

Lessons Learned.

But who wants to read that sob story! Rather, let's take a look at what I learned!

  1. I wrote my tests first! Before I attempted solving the problem I wrote tests for the solution, so at least I could prove it worked or not!
  2. I got ahead of myself in the coding - I started out thinking about what new ES6 feature I would use before effectively thinking through the problem.
  3. Start with the most obvious part of the problem - even if it is not the solution! (As with much development, start small and get the obvious results first before leaping to solutions... move in increments)

For example: The most obvious thing to do is to forget about excluding the one number in the multiplication and make sure you first get the products of all the numbers multiplied. From there, it is then far easier to focus on excluding the one number from that point than it is to get tangled up in a misguided attempt at solution.

  1. Drawing out a sketch of the idea for your solution helps you have a basis from which to work from and to which to go back to when you get caught up in going wrong.
  2. Get beyond embarrassment - I am embarrased that I struggled with this, the solution was so obvious once I got it (I am not sure if it was a good one - but it passed the tests.)

Possible Improvements.

Now that I found a solution, it is time to consider some improvements:

  1. I could use .reduce for the multiply function rather than a loop
  2. With .reduce, I could combine move that functionality into one function rather than two.
  3. I could look into using .map instead of a for loop.

On the bright side, I found a solution just as I was thinking I should step away and take a breather. Additionally, I discovered jest-extended which is a collection of useful and convenient assertions. Specifically I used .toIncludeSameMembers to ensure the outputted array matched the expectation.

That being said, I took a read through problem #3 and I feel more impostor-like than ever! I know on a surface level what a binary tree is but...

Update: I refactored my solution according to my suggestions, you can find my comments here.