Skip to content

Instantly share code, notes, and snippets.

@metame
Created Sep 4, 2015
Embed
What would you like to do?
cipher can be run with cli using node
// es2015
'use strict';
// run from cli as `babel rot13.js "string"`
let entry = process.argv[2];
entry => {
const alphabet = "abcdefghijklmnopqrstuvwxyz";
let outString = '';
for(var i=0; i<entry.length; i++){
let char = entry[i],
lowercase_char = char.toLowerCase(),
isUpperCase = ( char === lowercase_char ? false : true ),
index = alphabet.indexOf(lowercase_char);
if(index < 0){ // char is not a letter, return char
outString += char;
} else { // char is a letter
let translate = index - 13,
translated_index = ( translate > 0 ? translate : 26 + translate),
translated_char = alphabet[translated_index];
if(isUpperCase){
outString += translated_char.toUpperCase();
} else {
outString += translated_char;
}
}
}
console.log(outString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment