Skip to content

Instantly share code, notes, and snippets.

@JeffOgah
Created February 22, 2019 12:39
Show Gist options
  • Select an option

  • Save JeffOgah/c8b0731f964fba556c08e4f070ec9156 to your computer and use it in GitHub Desktop.

Select an option

Save JeffOgah/c8b0731f964fba556c08e4f070ec9156 to your computer and use it in GitHub Desktop.
JavaScript Algorithms and Data Structures Projects: Caesars Cipher
//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