- You need to write a function, which will take 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.
Use String.fromCharCode() to convert ASCII to English character.
Leave anything that doesn't come between A-Z as it is.
Solution ahead!
function rot13(str) {
var nstr="";
for(var i=0; i<str.length; i++){
//Checks if character lies between A-Z
if(str.charCodeAt(i) < 65 || str.charCodeAt(i) > 91) {
nstr += String.fromCharCode(str.charCodeAt(i));
continue;
}
//N = ASCII 78, if the character code is less than 78, shift forward 13 places
if(str.charCodeAt(i) < 78){
nstr += String.fromCharCode(str.charCodeAt(i) + 13);
}
else{
//Otherwise shift the character 13 places backward
nstr += String.fromCharCode(str.charCodeAt(i) - 13);
}
}
return nstr;
}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 > 91) {
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
}- A string variable
nstris 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)
If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @anuragaryan and @SaintPeter for your help with Algorithm: Caesar's Cipher
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)