Created
February 22, 2019 12:39
-
-
Save JeffOgah/c8b0731f964fba556c08e4f070ec9156 to your computer and use it in GitHub Desktop.
JavaScript Algorithms and Data Structures Projects: Caesars Cipher
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
| //A function which takes a ROT13 encoded string as input and returns a decoded string. | |
| function rot13(str) { | |
| //declare both alphabets halfs in order | |
| const rotFirstHalf = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"]; | |
| const rotSecondHalf = [ "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]; | |
| //function to check which half elements lies in | |
| //and return equivalent element | |
| function decodedStr(match) { | |
| if (rotFirstHalf.indexOf(match) >= 0) { | |
| return rotSecondHalf[rotFirstHalf.indexOf(match)]; | |
| } | |
| else { | |
| return rotFirstHalf[rotSecondHalf.indexOf(match)]; | |
| } | |
| } | |
| //match all alphabets and replace | |
| return str.replace(/[a-z]/gi, decodedStr); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment