Javascript: Caesar Cipher

This is a function to decode a message written in Caesar’s Cipher. It came about as my answer to a Javascript challenge in the Free Code Camp front-end web developer certification course.


function rot13(str) {
strArray = str.split("");
// A = 65, Z = 90
for (i=0; i < strArray.length; i++) {
var letterValue = str.charCodeAt(i);
// For letters A-M
if (letterValue > 64 && letterValue < 78) {
letterValue += 13; // For letters N-Z
} else if (letterValue > 77 && letterValue < 91) {
letterValue -= 13;
}
strArray[i] = String.fromCharCode(letterValue);
}
return strArray.join("");
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top