Algorithm Caesars Cipher - thelastmile/FreeCodeCamp GitHub Wiki
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
- You need to write a function, which will take a string encoded with Caesar cipher as a parameter and decode it.
- The one used here is ROT13 where the value of the letter is shifted by 13 places. e.g. 'A' ↔ 'N', 'T' ↔ 'G'.
- You have to shift it back 13 positions, such that 'N' ↔ 'A'.
Use String.charCodeAt() to convert the English character to ASCII.
try to solve the problem now
Use String.fromCharCode() to convert ASCII to English character.
try to solve the problem now
Leave anything that doesn't come between A-Z as it is.
try to solve the problem now
Solution ahead!
function rot13(str) {
// Split str into a character array
return str.split('')
// Iterate over each character in the array
.map.call(str, function(char) {
// Convert char to a character code
x = char.charCodeAt(0);
// Checks if character lies between A-Z
if (x < 65 || x > 90) {
return String.fromCharCode(x); // Return un-converted character
}
//N = ASCII 78, if the character code is less than 78, shift forward 13 places
else if (x < 78) {
return String.fromCharCode(x + 13);
}
// Otherwise shift the character 13 places backward
return String.fromCharCode(x - 13);
}).join(''); // Rejoin the array into a string
}
🚀 Run Code
- A string variable
nstr
is declared and initialized to store the decoded string. - The for loop is used to loop through each character of the input string.
- If the character is not uppercase English alphabets(i.e. its ascii doesn't lie between 65 and 91 ), we'll leave it as it is and continue with next iteration.
- If it's the uppercase English alphabet, we'll subtract 13 from it's ascii code.
- If the ascii code is less than 78, it'll get out of range when subtracted by 13 so we'll add 26 (number of letters in English alphabets) to it so that after A it'll go back to Z. e.g. M(77) ↔ 77-13 = 64(Not an English alphabet) +26 = 90 ↔ Z(90).
// Solution with Regular expression and Array of ASCII character codes
function rot13(str) {
var rotCharArray = [];
var regEx = /[A-Z]/ ;
str = str.split("");
for (var x in str) {
if (regEx.test(str[x])) {
// A more general approach
// possible because of modular arithmetic
// and cyclic nature of rot13 transform
rotCharArray.push((str[x].charCodeAt() - 65 + 13) % 26 + 65);
} else {
rotCharArray.push(str[x].charCodeAt());
}
}
str = String.fromCharCode.apply(String, rotCharArray);
return str;
}
// Change the inputs below to test
rot13("LBH QVQ VG!");
- An empty array is created in a variable called
rotCharArray
to store the character codes. - The
regEx
variable stores a regular expression for all uppercase letters from A to Z. - We split
str
into a character array and then use a for loop to loop through each character in the array. - Using an if statement, we test to see if the string only contains uppercase letters from A to Z.
- If it returns true, we use the
charCodeAt()
function and rot13 transformation to return the correct value, otherwise we return the initial value. - We then return the string with the character codes from the
rotCharArray
variable.
🚀 Run Code
function rot13(str) { // LBH QVQ VG!
return str.replace(/[A-Z]/g, (L) => String.fromCharCode(65 + (L.charCodeAt(0) - 65 + 13) % 26));
}
-
String.prototype.replace
function lets you transform aString
based on some pattern match (defined by a regular expression), and the transformation function (which is applied to each of the pattern matches). -
Arrow function syntax is used to write the function parameter to
replace()
. -
L
represents a single unit, from every pattern match with/[A-Z]/g
- which is every uppercase letter in the alphabet, fromA
toZ
, present in the string. - The arrow function applies the
rot13
transform on every uppercase letter from English alphabet present in the given string.
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
Thanks @anuragaryan @SaintPeter @vaskezu @abhisekp for your help with Algorithm: Caesar's Cipher
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories -- Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.