What is Caesars Cipher
? The particular constraint for this exercise is ROT13
algorithm (rotate by 13 alphabets), and only use upper case letters.
Here is my quick implementation:
function rot13(str) {
// LBH QVQ VG!
const input = str
.split("")
.map(item => item.charCodeAt())
.map(item => {
if (item < 65 || item > 90) {
return item;
} else if (item < 78) {
return item + 13;
} else {
return item - 13;
}
})
.map(item => String.fromCharCode(item))
.join("");
return input;
}
// Change the inputs below to test
console.log(rot13("SERR CVMMN!"));
The numbers inside the if statements of the code above are ASCII codes that correspond to the uppercase letters of the alphabets.