Skip to content

Instantly share code, notes, and snippets.

@sevperez
Created September 13, 2017 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevperez/792b9c0b4aa06e6b6ed662c90693f278 to your computer and use it in GitHub Desktop.
Save sevperez/792b9c0b4aa06e6b6ed662c90693f278 to your computer and use it in GitHub Desktop.
ls_course_210_rot13
// Encrypts a provided string using the Rot13 cipher
function rot13(string) {
function rotate(char, limit) {
var rotatedCharCode = char.charCodeAt(0) + 13;
if (rotatedCharCode > limit) {
rotatedCharCode -= 26;
}
return String.fromCharCode(rotatedCharCode);
}
var lowerCaseLimit = 122;
var upperCaseLimit = 90;
var result = '';
for (var i = 0, len = string.length; i < len; i++) {
if (string[i].match(/[A-Z]/)) {
result += rotate(string[i], upperCaseLimit);
} else if (string[i].match(/[a-z]/)) {
result += rotate(string[i], lowerCaseLimit);
} else {
result += string[i];
}
}
return result;
}
console.log(rot13('Teachers open the door, but you must enter by yourself.'));
// Grnpuref bcra gur qbbe, ohg lbh zhfg ragre ol lbhefrys.
console.log(rot13(rot13('Teachers open the door, but you must enter by yourself.')));
// Teachers open the door, but you must enter by yourself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment