Created
September 13, 2017 10:26
-
-
Save sevperez/792b9c0b4aa06e6b6ed662c90693f278 to your computer and use it in GitHub Desktop.
ls_course_210_rot13
This file contains 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
// 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