January 14th, 2021
On a cold, blustery afternoon, you find yourself wandering down an unknown street. Buttoning your coat tight under your chin, you look about to get your bearings. The waning sun shines off the windows of a familiar shape in the distance. It's that office building with the terrace on the roof. Home should be that way. Off you go.
You know where you are and where you're going. You can figure out your path along the way.
Similarly, with any coding challenge, whether you're building a thing or solving an algorithm challenge:
Before trying to figure out a solution, take a moment to first ascertain what information you have, and what you need to return from your code. As long as you know where you are and where you're going, you can figure out the middle bits along the way.
When approaching a task or problem look at two things:
What am I working with? Where am I?
What do I need to produce? Where am I going?
This defines how to structure the solution. I always write the beginning and ends of my solution (in JavaScript) first:
What shape is our data?
What sort of data structure is it? Object, string, number, array of objects? This will define what methods we can use to manipulate the data. In JavaScript, many useful Object Methods are different from available Array Methods.
(I always have to remind myself: For...in loops work for Objects. For..of loops are suitable for Arrays.)
How big is it? Will it always be an array of 10 objects, or could it be larger or smaller? This will determine how much looping you may want to do, to keep the function efficient. While we should always strive for efficient functions, this can become more important when dealing with potentially huge amounts data. Whereas perhaps we worry less when we always have an Array of 10 items.
How many arguments are there? Are all the arguments always provided? If arguments may sometimes be missing, it's important to handle that, possibly by setting a default in the function at the outset:
The default will only be used if that input is not provided.
Alternatively, you can use an if statement to check for the argument and alter your route based on that:
What does our code need to produce?
Is the input data structure the same as the output structure? For example, take in a String, change something and return a new String. For example, changing a blog post Title to a slug (part of a url) will involve removing any special characters (", ', !, etc) and kebab casing the words (word-word-word).
Or are they different? For example, taking in a String, checking for some quality, and returning a true or false. Like a Palindrome Checker will take in a String and return a Boolean (true or false) to say yes it is a palindrome, or no it is not.
You can define the output as an empty String (""), an empty Array ([]), null for an Object, or just some Number (usually 0 or 1 depending on what you want).
By defining the beginning and end of our functions first, we can create a smoother path to a solution. We know where we are and where we need to go.
On a cold, blustery afternoon, you find yourself wandering down an unknown street. Buttoning your coat tight under your chin, you keep your head down. Home should be around here somewhere. You pick a path at random and hope for the best.
I did this a lot when I first started trying to solve algo problems. I'd get overwhelmed trying to figure out the middle bits, wondering which methods I should use, loops, recursion,... ah! I wasted a lot of time muddling about in the middle.
Rather than jumping into solving the problem from the middle, look at the whole picture first. Take stock of the what the problem provides, and what it needs for a solution.
Once you get your bearings: know where you are, and which direction you should be heading; then, you can concentrate on finding your way. Before diving into writing code and solving the problem, take a few moments to solidify your understanding of the question.
Clarify what it provides, and what you need to return. This will allow you to find your way much more easily. In our first story, we find our location, know our destination and can therefore find a smooth path.
I hope this helps you on your way!
Happy coding!