One of my favorite cliche sayings is, "It's not the destination it's the journey." I especially like to remember this phrase on my quest to become a better JavaScript programmer. Lately, I have been devoting the first hour of my day to solving JavaScript algorithm problems on https://edabit.com/. This week was my first time attempting to solve problems that had a difficulty rating of, "hard." One problem in particular, the "Boomerangs" problem, proved to be especially difficult for me to solve. In this post I will share with all of you my solution to the problem. As always, feel free to share your solutions to the problem in the comments.
This is how the problem is stated on edabit.com, "A boomerang is a V-shaped sequence that is either upright or upside down. Specifically, a boomerang can be defined as: sub-array of length 3, with the first and last digits being the same and the middle digit being different. Create a function that returns the total number of boomerangs in an array." In my opinion, the use of the boomerang in this problem isn't really necessary other than to serve as a kind of mental model. What this problem is really asking you to do is find a pattern in an array. It also gives you some examples for reference.
As you can see from the examples, numbers that meet the condition for one sub-array can be re-used to form other sub-arrays.
Just to get the ball rolling, I like to write out the skeleton of my function with the necessary inputs and expected output.
We know we need a function that takes in an array of numbers, and that returns the number of boomerangs/sub arrays that satisfy the conditions stated in the problem. We need a place to keep track of the number of boomerangs, which is what the count variable is used for.
Since this is a problem that deals with array indexes, I opted to use a for loop. However, I am sure there is a way to solve this using a higher-order function like .filter(). I like using for loops because they force my mind to follow along with each iteration of the loop. Basically what we need to do is come up with a way to check if the numbers at each index in the array match the pattern we are given in the original problem. Since we have initialized our for loop to start counting at zero, or the first index in the array, we can easily check if the number at the third index is the same as the number of the first index, which we have specified in the condition of our if statement.
If it is, we increment our count variable by one.
This takes care of checking if the first and last digits in our "boomerang" are the same, but we also need to check if the middle digit is different. We can do this by adding another condition to the condition in our if statement with the && operator, which returns true if and only if both operands are true.
Here we are checking wether or not the number at the index next to i, which is the starting index/number on each iteration of the loop, is different. If both of these conditions evaluate true the count variable is incremented by one. One question that I had while solving this problem is what happens if there isn't an even number of sub-arrays that satisfy the conditions. After some research I discovered that JavaScript evaluates the indexes that don't satisfy the conditions as undefined. Apparently in other languages this can cause problems, but not in JavaScript. Thanks JavaScript!
I like to run all of the problems I solve with a program called https://runjs.dev/, which is a cool little program that allows you to run JS code instantly.
Our code works, and the problem is solved!
Everything looks easy in hindsight, but I assure you this problem took me awhile to solve. Avi Flombaum, one of the co-founders of the Flatiron School once said, "I've never heard of anyone getting worse at coding the longer they do it." Learning to code is a marathon, and not a sprint. If you put in the daily work, I promise you will get better. Here's to the celebration of our next small victory!
Happy coding!
Top comments (0)