So basically I did the last challenge, the Caesars Cipher. The algorithm is fine, works, yet I think that it is somewhat dirty (meaning using not a lot of methods at all, and over-complicating something simple).
function rot13(str) {
var strFinal = "";
for (var i = 0; i < str.length; i++) {
//This segment of code transforms from A to M to N to Z
if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 77) {
var charCode1 = str.charCodeAt(i) + 13;
charCode1 = String.fromCharCode(charCode1);
strFinal += charCode1;
}
//This other segment does the opposite
else if (str.charCodeAt(i) > 77 && str.charCodeAt(i) <= 90) {
var charCode2 = str.charCodeAt(i) - 13;
charCode2 = String.fromCharCode(charCode2);
strFinal += charCode2;
}
//That one sums to our final string non-alpha non-uppercase characters.
else {
strFinal += str[i];
}
}
return strFinal;
}
I’d like to read your opinions. Is this algorithm a good solution, or it is too heavy? What do you think?