Hello, my name is: Amy

This Week I Learned: 'codewars'!

This week I discovered this gem of a site called codewars. Use with caution: this site is highly addictive.

codewars Stats

Basically what happens, is that various people create programming challenges that you solve in order to get points. After you successfully solve each challenge, you get to look at other peoples solutions. Seeing how people approach the same problem and pick up new things is most of the fun!

Right now I've only done problems in JavaScript, but I have heard that CodeWars is a great way to learn new languages.

codewars Learning


By looking at someone else's answer I learned about 'Set', which is a way of maintaining and accessing unique values in a collection. I stumbled though an answer using array.reduce() and then saw someone else have a sleek answer using this thing called Set.

Fancy string.replace()

A lot of the problems seem to be a form of string manipulation. In one problem of formatting a string of names, I learned about a slick way of using replace.

The problem was basically to format a list of names so the last one had and instead of a comma:

Amy, Bob, Mary, John.

Amy, Bob, Mary and John

My Way:

I solved this by finding the index of the last comma, and then assembling back together with substr(). Passable, but a bit clunky.

lastComma = list.lastIndexOf(',');
list.substr(0, lastComma) + ' and' + list.substr(lastComma + 1);

Cool Way:

In this code, they find the last comma (and name) with a regular expression. The regex states that it must have a , before, that the word should be captured (\w), and that it should occur at the last part of the string $. Once they found it, and captured the word (by using parenthesis), they can use the captured word in the replacement parameter (that's the $1). Cool, right?!

dialog.replace(/,\s(\w+)$/, " and $1");

So. Much. Fun.