Skip to content

Instantly share code, notes, and snippets.

@erictleung
Created April 23, 2016 04:17
Show Gist options
  • Save erictleung/e1819278247ea9f685d33f48ca8bf9fa to your computer and use it in GitHub Desktop.
Save erictleung/e1819278247ea9f685d33f48ca8bf9fa to your computer and use it in GitHub Desktop.
Caesars Cipher
function rot13(str) { // LBH QVQ VG!
var output = "";
for (var i = 0; i < str.length; i++) {
console.log("We are in loop " + i);
var ascii = str[i].charCodeAt();
console.log("Ascii value: " + ascii);
// Check if character is alphabetical or not
if (ascii <= 90 && ascii >= 65) {
console.log("Character is part of alphabet");
// Convert character 13 characters over
var move = ascii + 13;
console.log("Moved char: " + move);
// Loop through alphabet if beyond limits
if (move > 90) {
output += String.fromCharCode(move - 26);
} else {
output += String.fromCharCode(move);
}
console.log("New string: " + output);
} else {
// Return value if anything else by alphabetical
output += str[i];
}
console.log("\n");
}
console.log("\n");
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment