I SOLVED “Caesars Cipher” IN 20 MINS WITHOUT LOOKING ANYTHING UP!
And I think my solution is pretty clean too.
I know to most of you this barely constitutes an “accomplishment”, but the Basic Algorithm Scripting challenges were kicking my rear. I only solved 3 or 4 of them without looking anything up and those all seemed like they were the easy ones of the bunch.
When I saw the instructions for “Caesars Cipher” I thought there was no way I’d be able to do it without some help, but I gave it a shot anyway and wrote the code at the bottom without any assistance or googling.
After struggling with many of the challenges it feels great to sit down, logic the problem out and code it cleanly on my own. Just thought I’d share.
Spoiler Warning for anyone who hasn’t done “Caesars Cipher” yet. .
function rot13(str) { // LBH QVQ VG!
var coded = str.split("");
var charC = [];
var charCA = [];
var decoded = [];
for (var i = 0; i < coded.length; i++) {
charC.push(str.charCodeAt(i));
}
for (var l = 0; l < charC.length; l++) {
if (charC[l] === 32 || charC[l] > 91 || charC[l] < 65){
charCA.push(charC[l]);
}
else if (charC[l] +13 >= 91) {
charCA.push(charC[l]-13);
}
else {
charCA.push(charC[l]+13);
}
}
for (var m = 0; m < charCA.length; m++) {
decoded.push(String.fromCharCode(charCA[m]));
}
return decoded.join('');
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");