Asking for feed back on Caesar's Cipher Challange solution

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 :slight_smile:

(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("");
    }
1 Like

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!

Good luck with the rest of the algorithms!

1 Like

thanks for your feedback! i appreciate it! i’ll pay more attention to variable name from now on :smile:

Your algo solution is better than mine to be honest. :wink:
I’m potentially going to have to repeat myself because everyone does this.

//**Bad:**
var i;
for (i = 0; i < arr.length; i++) {
// **Better Code:**
var i;
var len = arr.length;
for (i = 0; i < len; i++) {

Suggested by:
http://www.w3schools.com/js/js_performance.asp

I made a separate forum post about this now.

2 Likes

Makes sense :thumbsup:

thank you, nice tips by the way
I will definitely keep it in mind when using for loops next time

2 Likes