Created
September 4, 2015 21:30
-
-
Save metame/e621470b1c12d6c34caa to your computer and use it in GitHub Desktop.
cipher can be run with cli using node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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