A decoder ring CLI for secret messages

I’ve recently been using Free Code Camp for exercises to continue my learning of JavaScript and NodeJS. Some of my favorite challenges are the algorithm scripting ones. They give you a specific process to accomplish along with expected results that your code is tested against. Once you get your code to pass all of the tests you can move on to the next one. They are super fun.

The latest one that I’ve finished challenges you to decode a set of strings using a ROT13 cipher. You have to figure out how to run through the string, taking in each character, accounting for punctuation, and then returning the appropriate letter based on the ROT13 system.

In the description of each challenge there are some tips on how to accomplish it with suggested methods to try using. But, you really are free to do it however you want. As long as the tests pass, you pass the challenge. I eventually got the challenge completed, but didn’t like my method. So, I checked out the FCC Gitter channels to see how others had accomplished it. One person had a really cool idea of creating an object to be a cipher to compare the given letters to and return the corresponding letter. I tried it out myself and was able to get my own version implemented.

This got me thinking about how it could be fun to have a little app the encodes or decodes messages using this system. It would be like a decoder ring for your terminal. This coupled with my recent discovery of VorpalJS and I had my next idea for a CLI project. Side note, I’m currently obsessed with the idea of making command line apps. It is kind of my jam.

So, I got to work on the project and it came along faster than I thought. As I was making the decode command I got to thinking about how I don’t really have to make separate functions for decoding and encoding. All I’m really doing is converting the letters from one to the other. So, I can just make a single conversion function to run for each command.

The code looks like this:

var cipher = {
"A": "N",
"B": "O",
"C": "P",
"D": "Q",
"E": "R",
"F": "S",
"G": "T",
"H": "U",
"I": "V",
"J": "W",
"K": "X",
"L": "Y",
"M": "Z",
"N": "A",
"O": "B",
"P": "C",
"Q": "D",
"R": "E",
"S": "F",
"T": "G",
"U": "H",
"V": "I",
"W": "J",
"X": "K",
"Y": "L",
"Z": "M"
};

function conversion(str) {
var converted = [];
var original = str.toUpperCase().split("");
for (var i = 0; i < original.length; i++) {
if (cipher[original[i]] === undefined) {
converted.push(original[i]);
} else {
converted.push(cipher[original[i]]);
}
}
return converted.join("");
}

I am pretty happy with it in its current state. Like my weather CLI I will revisit it later on and see what else I can add to it. If you want to check out the project you can find it here on my Github page.

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