function rot13(encodedStr) {
var codeArr = encodedStr.split(""),
decodedArr = [], char;
function isLetter(char) { return /[a-z]/i.test(char); }
function cipher(letter) {
var charCode = letter.charCodeAt(0), // ASCII
exceedsM = (charCode<97)? charCode>77 : charCode>109;
return (exceedsM) ? charCode-13 : charCode+13;
} // Transforms the letter into a new ASCII code
for (var i = 0; i < codeArr.length; i++) {
char = codeArr[i]; // Cache the current character
if(isLetter(char)) {
char = String.fromCharCode(cipher(char));
} // Re-Assigns the letter for the ciphered new one
decodedArr.push(char);
} // Finished iterating and pushing
return decodedArr.join("");
}
// Example:
rot13("FreeCodeCamp");
// -> "SerrPbqrPnzc"
// Decoding:
rot13("SerrPbqrPnzc");
// -> "FreeCodeCamp"
An useful FreeCodeCamp bonfire to encode-decode with ROT13.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.