Skip to content

Instantly share code, notes, and snippets.

@Shaikot007
Last active July 12, 2021 11:30
Show Gist options
  • Save Shaikot007/a7987bb266ace7f8ebd85c016ab456fa to your computer and use it in GitHub Desktop.
Save Shaikot007/a7987bb266ace7f8ebd85c016ab456fa to your computer and use it in GitHub Desktop.
Caesars cipher. (freeCodeCamp JavaScript algorithms and data structures challenge)
function rot13(str) {
newstr = str.split("");
var finalstr = newstr.map(function(letter) {
lettervalue = letter.charCodeAt(0);
if (lettervalue < 65 || lettervalue > 90) {
return String.fromCharCode(lettervalue);
}
else if (lettervalue < 78) {
return String.fromCharCode(lettervalue + 13);
}
else return String.fromCharCode(lettervalue - 13);
}).join("");
return finalstr;
};
rot13("SERR PBQR PNZC");
// should decode to "FREE CODE CAMP"
// rot13("SERR CVMMN!");
// should decode to "FREE PIZZA!"
// rot13("SERR YBIR?");
// should decode to "FREE LOVE?"
// rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");
// should decode to "THE QUICK BROWN DOG JUMPED OVER THE LAZY FOX."
// A B C D E F G H I J K L M
// ROT13
// N O P Q R S T U V W X Y Z
// HELLO
// ROT13
// URYYB
// ASCII Table
// freeCodeCamp javaScript algorithms and data structures challenge link is given below:
// https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment