DEV Community

Sarah
Sarah

Posted on

Fighting the Fear of Code Challenges

First things first, one thing I wished I did more of while I was in bootcamp was code challenges. I kept putting them off to work on labs, study, watch tutorials, or anything else. Soon enough, I realized the reason was that they confused and frustrated me.

Fast forward to 3 months after graduating (yay! 😄) - I'm now working on at least one Codewars challenge a day. By no means am I a pro at solving these, I'm still working my way through 8kyu, 7kyu and 6kyu challenges and my code can use a lot of love (and refactoring!) BUT I have noticed my improvements since I started practising more, and I am proud of how far I've gone.

In this blog post I'll quickly go over a solution for one of the last code challenges I completed, but before I do, I want to quickly explain why I avoided code challenges before and how I battled that fear.

Problem: Code Challenges Scared Me

I'm sure a lot of you can (or once did) relate to this! Reading the details overwhelmed me, and I lacked knowledge on what methods were available for me to use. So I would get frustrated and not know where to start. 😩

Solution: Baby Steps

Start small, start simple, practice often and let go of your ego. Coming from a bootcamp I initially wanted to jump into more difficult challenges, but I didn't. I asked the Twitter dev community for some advice, and there were recommendations to start easy, so that's what I did. Start easy, build confidence and the challenges won't be so intimidating.

Challenge Time: Split Strings

You can find the challenge here if you'd like to give it a shot.

Goal: Create a function that splits a given string into pairs. If the string has an odd number of characters, add an underscore('_') to the final pair.

Reading this challenge my thought process was this:

  1. Check if the character count is even or odd
  2. If the character count is even do this
  3. If the character count is odd do something else

So my initial code looked like this:

function solution(s) {
    // Check if characters are even or odd
    // If characters are even do this
    // If characters are odd do this
}
Enter fullscreen mode Exit fullscreen mode

TIP: There is no such thing as too much pseudo code!

So step 1, check if the character count is even or odd. I did this by taking the length of the string, and using the modulo operator to see whether or not the remainder after dividing by 2 was equal to 0. If there is no remainder, the number is even.

function solution(s) {
  // Check if characters are even or odd
  if (s.length % 2 === 0) {// checks if characters are even
    // If characters are even do this
  } else {
    // If characters are odd do this
  }
}
Enter fullscreen mode Exit fullscreen mode

From here I used the match() method which searches the string with RegExp and returns the matches as an Array. I used RegEx101.com to test. I liked this tool because it gives an explanation of what your regular expression is doing - that was very helpful for me.
RegExp Explanation

return s.match(/../g};
Enter fullscreen mode Exit fullscreen mode

So now that I have the first portion returning as expected, the else portion has these goals:

  1. Add an underscore('_') to the last pair
  2. Return pairs from the given string
const newString = s + "_"; // add an underscore('_')
return newString.match(/../g); // return pairs
Enter fullscreen mode Exit fullscreen mode

My final solution (minus the pseudo code) is this:

function solution(s) {
  if (s.length % 2 === 0) {
    return s.match(/../g);
  } else {
    const newString = s + "_";
    return newString.match(/../g);
  }
}
Enter fullscreen mode Exit fullscreen mode

One of my favourite things about Codewars is that you're able to see other solutions after. I find this very helpful. I can see where I could have improved, what to refactor, learn about different methods I never knew existed. And like I mentioned above, I am no pro at these. The solutions were definitely more elegant than mine 😅, but I wanted to share what I came up with and explain my thought process (before I refactor it).

If you're struggling with or avoiding code challenges, take it from someone who used to dread them but now does them for fun 😆 - you can learn to love them with confidence. Start easy, be consistent, read other solutions (after you've completed it yourself), and most importantly, pseudo code the hell out of it! The more you explain to yourself what you're doing, the easier it'll be to keep your thought process on track and not get overwhelmed. And of course, always celebrate your wins, big or small.

Top comments (3)

Collapse
 
egilhuber profile image
erica (she/her)

Codewars looks super fun! I'm a recent bootcamp grad too, and keeping all those skills fresh is definitely important.

Collapse
 
heysarahpaz profile image
Sarah

Congrats on graduating! You're absolutely right! And yes, Codewars is fun - frustrating at times lol but fun!

Collapse
 
exenestecnico profile image
Ernesto