Problems Solved: Part Two

Paul Andrews
Nov 2 · 4 min read
Photo by Markus Spiske on Unsplash

Another day, another coding challenge. That mantra has been colouring my life since I’ve written last. And I’m happy about it. Each day I’m solving new problems and strengthening my coding skills. I’m much more comfortable with core array methods and manipulating data types in order to reach a desired outcome.

However, you won’t see some of the other coding muscles I’ve put to work over the past few weeks here. I feel increasingly comfortable with the concept of Promises, the this keyword (everyone’s favourite), ES6 features, and making GET requests. I’ve also started a React udemy course which has been fun, especially after focusing on vanilla JS for weeks. It’s unfortunate I won’t touch upon those skills here, but maybe they’re worthy of another blog post at another time?

Once again, feedback is always welcome on the following solutions! It’s incredible to see how much my code can be refactored in order to boost efficiency, readability, and performance. I learn so much from 1: getting something to function, and then 2: seeing how I can make the code work so it’s easier on the eyes and browser. Seeing other people’s solutions is a key part of that learning.

// Funny String Attachment

// Turn a string such as this ‘abcd’ into this ‘A-Bb-Ccc-Dddd’

Alright, this one was a bit tricky to solve. It definitely forced me to think through how I would manipulate the original string. The function’s result needed the string’s characters to be capitalized, multiplied, and include hyphens…cool.

I’ve found that in many coding challenges involving the manipulation of string order or value that it’s smart to spread the string into an array. In this function I first converted the string .toUpperCase() and then spread that new string into an array. Now that we have our array, manipulation becomes much easier thanks to an array of methods available to us (see what I did there?).

So, now what? I need to somehow use this array to add more characters based on index position AND add hyphens. An array’s .map() method was the straightforward choice here, as it would return a new array of modified values. Cool. But how could I add more characters? I did some research and discovered the string method .repeat(). It repeats any string it’s attached to for the number of times passed into it as a parameter. Using the .map() method’s second parameter i as index, I was able to return a new array where each value was the original capitalized letter. Those values were concatenated with the letter .toLowerCase() and followed by the same letter .repeat(i) number of times.

Now we have an array with each of the values we need to make up our final, modified string. To turn that into a string I simply created a new variable and set it’s value as the array joined by (“-”). The function works as expected once it returns that final string.

I was reminded of something really cool as I submitted this challenge to a popular code practice website. One of the example solutions they shared was about one line of code and chained each of the methods that I used together into one return statement. It was impressive. It was also something that I forgot we could use in JavaScript. I don’t think my mind is there yet as a developer. It’s still makes sense for my mind to think out solutions in steps that lead to a function like mine above. But I’m confident that over time my eye for refactoring code into something more efficient and readable will improve.

// Is email?

// Write a function that takes in a string, and verifies that it could possibly be a valid email based on common assumptions of what an email should look like.

What’s this? A coding challenge that looks more than remotely like something you might see in the wild? I was excited to tackle this one.

When I thought about how to solve this problem the first thing that came to mind was Regex. It hadn’t let me down yet and it didn’t disappoint in this solution. Regex and it’s method .test() is a readable and efficient way to solve any verification problem. So long as your Regex is properly understood (can help readers by commenting what your Regex variables check for), I think it’s appropriate to use.

And so, the first thing to do in solving this problem was to define my Regex variables. Email addresses don’t have any spaces and so a string passed in that contains white space should return false immediately. My first Regex variable checks for that white space. My second is more nuanced. I decided that most common email addresses point to a domain with no non-letter characters. That is, emails such as ‘@gmail’ or ‘@yahoo’ are more common than ‘@123’ or even ‘@!me’. My second Regex variable verifies if the first character after an @ character is a letter character.

From here, I used the power of Regex’s .test() method and conditional if…else statements. The function will return false if the whitespace.test(str) is true, return true if charAfterSymbol.test(str) is true, and return false if there’s a string that doesn’t neatly fit into either (for example a string like ‘hello’). Calling the function with sample strings and testing that conditional logic returns accurate the boolean values. Another problem solved.

So there you have it! Two solutions to two straightforward but tricky challenges. Once again, let me know your feedback on my solutions and share what you might have done differently.

Paul Andrews

Written by

web developer || Toronto || “The struggle itself toward the heights is enough to fill a man’s heart.”

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade