  # ROT13 Algorithm ROT13 is a specific instance of the Caesar cipher, which replaces each letter of the alphabet with another letter a set number of spaces forward in the alphabet.  If the end of the alphabet is reached, one begins counting at the beginning of the alphabet again.  The ROT13 algorithm is it's own inverse, since there are 26 letters in the basic Latin alphabet.  This means that the output from the algorithm can itself be run through the algorithm to get the original string back (Source: Wikipedia, "ROT13").  The algorithm can be tested below by typing a string into the topmost field above the lock picture.  Capital letters will be converted to their ROT13 value, while all other characters will simply pass through intact.  Below are some strings you might want to try:

JavaScript
``````function rot13(str) {

// 1- Convert every character to the number portion (charCode) of the that character's HTML entity ---
//helpful map of HTML entities: https://unicode-table.com/en/
//create an array containing all characters
var indivChars = str.split("");
//create an array of only every character that is not a capital letter
var nonLetterRE = /[^A-Z]/g;
var nonLetters = str.match(nonLetterRE);
//create an empty array that will contain all the numerical values for the characters
var numEntityArr = [];
//cycle through array containing all characters & convert the capital letters to their character code
//if a character does not belong to A-Z, assign it a value of 0
for (let i = 0; i < indivChars.length; i++) {
var numberEntity = indivChars[i].charCodeAt(0);
if (numberEntity >= 65 && numberEntity <= 90) {
numEntityArr.push(numberEntity);
} else {
numEntityArr.push(0);
}
}

// 2 - shift each character code 13 positions numerically ---
var shiftedArray = [];
var counter = 0;
for (let i = 0; i < numEntityArr.length; i++) {
//if the character code represents a letter within A-M, add 13 to the character code
if (numEntityArr[i] >= 65 && numEntityArr[i] <= 77) {
numEntityArr[i] = numEntityArr[i] + 13;
//push this new number into an array, along with prefix and suffix that make the character code an HTML entity
shiftedArray.push("&#" + numEntityArr[i] + ";");
//if the character code represents a letter within N-Z, subtract 13 from the character code
} else if (numEntityArr[i] >= 78 && numEntityArr[i] <= 90) {
numEntityArr[i] = numEntityArr[i] - 13;
shiftedArray.push("&#" + numEntityArr[i] + ";");
//if the character code is not A-Z or 0, simply add prefix and suffix to make it an HTML entity & push it into the array
} else if (numEntityArr[i] !== 0) {
shiftedArray.push("&#" + numEntityArr[i] + ";");
//if the character code is 0, replace it with the corresponding index of the nonLetter array created above
} else if (numEntityArr[i] === 0) {
shiftedArray.push(nonLetters[counter]);
counter += 1;
}
}

// 3 - return correct letters ---
//join in a string the html entities and non A-Z characters pushed into shiftedArray
var entityString = shiftedArray.join("");
//convert the HTML entities into characters, while passing all other non-entity characters through
//credit: http://stackoverflow.com/questions/7394748/whats-the-right-way-to-decode-a-string-that-has-special-html-entities-in-it
function decodeHTML(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
var decodedHTML = decodeHTML(entityString);
return decodedHTML;
}``````