MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 6
Problem: Caesars Cypher ROT13.
Problem Definition: Caesars Cypher ROT13 decodes word by shifting it thirteen places backward.
Algorithm
The given string is splitted into an array/list of characters.
I looped through each character and get its ASCII code.
I then checked if each character ASCII code is between A-Z and return the character as it is if it's not between A-Z .
I shifted character code of character whose ASCII code fall between A-Z by 13 and return the corresponding character at that position.
For character with ASCII code lesser than 78, I shifted them forward by 13 places so as to fall between A-Z .
JavaScript Code
function ROT13(str) {
return str.split(" ").map.call(str, function(char){
x = char.charCodeAt(0);
if (x < 65 || x > 90) {
return String.fromCharCode(x);
}
else if (x < 78) {
return String.fromCharCode(x + 13);
}
return String.fromCharCode(x - 13);
}).join(" ");
}