Codewars #2: Your order, please



It’s that time again where I talk about another challenge on codewars. Seriously, if you love programming and want to improve go join. It’s pretty rad. Now enough promoting code wars to my imaginary readers…

In today’s challenge, I had to deal with strings. Here’s the problem set:

sounds easy enough right? To be honest, I was pretty hesitant approaching this challenge. Because string parsing isn’t my strong suit in Javascript. [I mastered it in python, javascript not so much]. Now before I go on my rambles and thought process to how I solved this problem, I know it’s not the best approach because after calculating the time it took O(n) to compute which isn’t good but as my algorithm professor said, first make it work, then think of how to improve it. So now let’s make it work.

First thing I thought of is to split the string into individual words because I want to check each work and the number it contains. That was easy since the string object in javascript has a method for it. Splitting string complete:

Now I had my array of words, I need to parse them to check their content … Damn it. I tried searching online if there’s a function, or perhaps an algorithm., but turns out I need to use regular expression. This reddit post and stackoverflow helped me out. Now I was able to also extract the number from the value (I wanted to extract it in order to sort it.)

I wanted to check my work first to see what the output that I’m getting. So I ran the script and checked Chromes developer tools and the result was odd…

Wow… what’s happening? I went over to Mozilla developer network (because they have amazing documentation) and read about the match method. As you can see the methods returns an array.



Okay so we’re entering the realm of second dimension [that’s what I love to call nested arrays or 2d arrays] but here’s the thing, they’re mixed up data types. I have a string and two objects… I’m sweating because I’m thinking, how will I use the sort object on them… But yet again, the StackOverflow gods were there to the rescue. After reading through a couple of posts there were the two [1,2] that helped. We’re almost there. Now I sorted them out and concatenated them into a string to produce the desired output. Now all is good. All I have to do is submit the code on codewars…

Saaay whaaaat?! Why? That’s odd… as you can see from the error message it says expected, and got. They’re the exact same. What’s up bro…

And here’s where I started to get all frustrated. I do not get it. It’s the exact same thing. I ran it over and over with different test cases

and still got a fail. Then it hit me… could it be?

that last line of words += ‘ ‘; can that be the problem? Because it’s adding an extra space? [I will seriously smash my screen if that’s the case] so I adjusted that and added a condition to check if it’s the last word. And yup that was the problem… The code past. I do not know whether to be happy or annoyed.

disclaimer: no computer screens were harmed in the process of coding.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *