CodeWars: Improve your code skills!
I’ve done a lot of reflection since not only graduating from the bootcamp but also finishing school. I was going through a difficult time with having to juggle a bootcamp, school and also raising my beautiful baby.
It just did not seem like there were enough hours in a day. I think I failed to realize the great resources that are out there to become a better developer. I think that if I knew of them, then, I would have been a much more rock solid developer coming out the bootcamp. Currently, I feel like I’m playing catch-up trying to improve my data structures and algorithms.
One resource in particular that has crossed my path as of late is CodeWars. I just so happened to searching through twitter trying to find more information on a tech fellowship I applied to. The name CodeWars peaked my interest and I signed up for an account.
What I also like about this site is that if you try to view the solution without completing the problem, it prompts a message basically saying you won’t get any kyu (this website has some kind of leveling up system). I personally like how this message became a little reminder to persevere and try to solve the problem on your own. Who wouldn’t want to level up?
This website seems like a great resource to building a strong foundation in data structures and algorithms. One of these questions were reminiscent of a question that I had actually gotten on a mock technical interview I did. However, I realized that I had since forgotten the correct built-in methods to reach a solution so I of course had to read up on some documentation.
I decided to run through that CodeWar problem today with you all!
It’s time to Reverse some strings!!!
The goal of this problem is to complete the solution so that it reverses the string to be passed into it.
The solution starts of with this function and you can even see the sample tests below:
One may not know where to start but the easiest way to think about is that you can’t reverse a string. The first step is to use the split() method. The split() method splits a string into an array of substrings.
However, it’s important to keep in mind two things:
If I use the split() like so: str.string(), it will just simply turn my entire word into an array. I want each and every letter to be contained in this own strings in this array...I want substrings!!! This will effectively let me reverse the array to the desired solution.
Using str.string(“”), will turn our world into an array of substrings with each letter in its own string.
This is a great start! If I pass “world” as an argument, the variable splitString will return: [“w”, “o”, “r”, “l”, “d”]. Now the next step is simple reversing this array.
We can use the .reverse() method for this. Simple, make a new variable call our splitString variable and call the reverse() method on it. This should return [“d”, “l”, “r”, “o”, “w”].
Lastly, here is the the easy part. We can use the .join() method to take all our elements of our array and join it into a string! Easy peasy lemon squeezy.
Simply, create a new variable that is equal to our reverseArray variable using the join() method on it and that’s a wrap. We simply return our variable this and our test cases shall all pass! Check out the full solution below:
Cool! However, there is a much beautiful, easier on the eyes way to code this…there always is. I can write this all on one line instead of making all these useless variables. A simple return statement with all these methods attach honestly does the trick.
Literally… that’s all there is to it. As time goes on, our code becomes shorter and shorter because… we just get better at it..