Hi, i just completed caesar’s cipher challenge and i want to ask for your feedback whether my solution had programming best practice or not and what can i improve on it?
i apologize for my english, it is not my first language
(SPOILER)
function rot13(str) {
var arrS = str.toUpperCase().split("");
for (var x = 0; x < arrS.length; x++){
if (arrS[x].match(/[A-M]/g) !== null){
arrS[x] = String.fromCharCode(arrS[x].charCodeAt(0)+13);
} else if (arrS[x].match(/[N-Z]/g) !== null) {
arrS[x] = String.fromCharCode(arrS[x].charCodeAt(0)-13);
}
}
return arrS.join("");
}
Hey your English is great so don’t worry about it!
I think your algorithm looks good. Your use of regex makes it easy to read and reason about.
The only improvement I can see is to use a more explicit name for your array variable arrS, like arrStr. I know it’s a tiny difference! lol that’s because the code is good!