Becoming More Than A Beginner

I imagine most everyone who sets out to learn coding will at some point look at someone else’s code. I try to do this as much as possible, and I especially like to see how others have solved the same problems I’m working on. It is in these moments that I often realize just how far I have to go.

I Am Relatively New To Javascript

And my code shows it. Take for example this exercise from the beginning algorithm section of the course I’m working on:

One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.

A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus ‘A’ ↔ ‘N’, ‘B’ ↔ ‘O’ and so on.

Write a function which takes a ROT13 encoded string as input and returns a decoded string.

All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.

My solution works. I know that if something works that’s the important thing, but I also know that for every solution I come up with, there’s a more elegant solution possible.

function rot13(str) { // LBH QVQ VG!
  var arrA = str.split('');
  for (var i = 0; i < arrA.length; i++) {
    if (arrA[i].match(/[a-z]/i)) {
      arrA[i] = translate(arrA[i]);
    } else {
      continue;
    }
  }
  return arrA.join('');
}

function translate(char) {
  var x = char.charCodeAt(0);
  var y = 0;
  if ((x - 13) < 65) {
    y = (x - 65);
    x = 13 - y;
    x = 91 - x;
  } else {
    x = (x - 13);
  }
  return String.fromCharCode(x);
}

rot13("SERR PBQR PNZC");

All things considered, it’s not a terribly difficult problem and it doesn’t necessarily require an overly complex solution. However, if I were sitting in a job interview and they asked me to code a solution to this problem (I don’t know if that would ever actually happen, as I’ve interviewed for exactly zero coding jobs at this point in my life) I would feel a lot better giving a more elegant solution just to show that my knowledge of Javascript is a bit deeper than the average beginner could learn in an afternoon.

Were I to solve this problem today, I would likely use Array.reduce() in place of the for (...) {...} loop, and rather than implementing a separate function to handle the math I’d use an inline function since I don’t need to call the math function anywhere else. So something like:

function rot13(str) { // LBH QVQ VG!
  return str.split('').reduce(function(a, b) {
    if (b.match(/[a-z]/i)) {
      var temp = function(x) {
        if ((x - 13) < 65) {
          x = 91 - (13 - (x - 65));
        } else {
          x = (x - 13);
        }
        return String.fromCharCode(x);
      };
      a.push(temp(b.charCodeAt(0)));
    } else {
      a.push(b);
    }
    return a;
  }, []).join('');
}

rot13("SERR PBQR PNZC");

As you can see, there’s not a great reduction in the number of lines of code, but the Intermediate Solution takes advantage of more advanced techniques which I feel would show a greater understanding of Javascript. I feel the math bit in the middle is the next portion to be improved upon, and I can’t wait to learn those things which would allow me to improve upon it.

What Does This Have To Do With Looking At Other’s Code?

I think Quincy Larson, creator of Free Code Camp, summed up my experience very well in this Youtube interview when he says, “Free Code Camp is essentially like self-learning together.” While there are many others currently doing the same course-work I’m doing, and many, many others who have already completed the course, when it comes down to it I’m learning all of this on my own. Sure, in the challenge text they may tell you to read some reference material, but anyone who can look at this reference page and then know how Array.reduce() works is a lot smarter than I am. So instead, I read others’ code to see how they use it and learn from their successes. Also, as I’ve mentioned before there are chatrooms, forums and message boards available to those needing help as well, but I generally try to find the solution on my own.

It’s fun to go back and re-solve old problems in new ways, and helps me see my progress. Having that kind of perspective really helps to keep me motivated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s