/ codewars

A Week of JavaScript

Coming to the end of the first week of the Learn JavaScript course and things are progressing nicely. After alternating between JavaScript: The Definitive Guide and Professional JavaScript for Web Developers, I find myself using the Definitive Guide almost exclusively. The reason for this is purely based on code examples. While Pro JS does an admirable job of explaining the concepts, Definitive is simply packed with code syntax. Where Pro JS needs a paragraph or two to explain something, Definitive does the same job in just a few lines of code.

logoAround the beginning of my JavaScript course,  I discovered Codewars; a community-driven tool built around the concept of Katas. After signing up and inputting your programming experience, you’re presented with a problem and (if you’re lucky) a starting line of code. Then you’re on your own. Comparing my own code to that of other codewars users was a humbling experience and drove home just how ‘noob’ I actually am. No matter how short and succinct my own code was, there was always a “JavaScript Grand Master” who managed to solve the same problem in two lines (or less!).

Out of the ten Katas I attempted, I solved maybe two or three and had to resort to skipping the others. A particularly nasty one asked for a function that takes in a positive number and outputs the factorial. I had no chance. I closed the site, dejected and went back to reviewing Objects.

Later in the week, I found myself reading an example that explained the very function that I was looking for! I had no excuse now, it was time to solve that codewars problem!

The solution uses the concept of recursion in which the function calls itself (from within) depending on a condition. Here’s an example:

function recursive(x) { if (x > 5) { return true; } else { console.log("x is " + x); return recursive(++x); } }

in this basic example, the function will repeatedly call itself (after incrementing x by 1) every time x is less than 5. This concept is the foundation for the aforementioned factorial function. A solution to the factorial problem can be written like this.

function factorial(n) { if (n < 0) { return null; } if (n <= 1) { return 1; } return n * factorial(--n); }

Just as I was about to post this submission to codewars, I came across the ternary (or conditional) operator in the Definitive book. The ternary operator is simply another way of writing a simple if statement and the MDN link explains it perfectly.

Armed with this new information, I re-factored my code and ended up with something that looks worthy of being submitted to codewars!

function factorial(n) { return n < 0 ? null : n <= 1 ? 1 : n * factorial(--n); }

As you can see, another “nested” ternary operator is invoked when n is not less than 0. It’s not quite “JavaScript Grand Master”, but it’s “a lot less noob” than it was a few days ago! There’s no doubt that codewars will be an essential tool for putting all of the course theory into practice. It’s a nice (and fun!) way to reassure me that what I’m learning is actually sinking in.

One week down, five to go!