After the relative failures in my coding problem yesterday - I decided to attempt the "Possible Improvements" suggested. Having understood the problem, it easy to refactor the solutions using more modern ES6 constructs such as .filter, .reduce and .map.
Lessons Learned.
- Once you have a solution, it becomes easier to reason about improvements if you have an idea of what avenues you have for improvement.
- Tests really help you find bugs in your code! (During the refactor, after my first pass I thought I had a successful refactor, but a failing test showed me that I had a flaw in my reasoning!)
- It helps to step through your failing code step by step to understand where and why you are going wrong!
- You cannot use a conditional inside of .reduce, you first need to .filter your set on the condition you have in mind and then .reduce from there.
Possible Improvement.
Combine the two functions into a more succinct solution.
All things considered, I still feel a long way off from being half-way decent at algorithms, but I am making progress, no matter how small the steps!