Skip to content

Instantly share code, notes, and snippets.

@cefaijustin
Created August 19, 2018 01:55
Show Gist options
  • Save cefaijustin/c847e516e155206f1ec8c6dc032a01f6 to your computer and use it in GitHub Desktop.
Save cefaijustin/c847e516e155206f1ec8c6dc032a01f6 to your computer and use it in GitHub Desktop.
Caesar Cipher Algorithm
function rot13(name) {
if (name.length === 0) return "Submission cannot be empty.";
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
let res = [];
name = name.toLowerCase().split("");
for (let i = 0; i < name.length; i++) {
for (let j = 0; j < alphabet.length; j++) {
if (name[i] === alphabet[j]) {
if (alphabet[j + 13] === undefined) {
res.push(alphabet[j - 13]);
} else res.push(alphabet[j + 13]);
}
}
}
return res.join("");
}
console.log(rot13("HELLO"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment