Can I get some advice on my solution for Caesars Cipher?

Can I get some advice on my solution for Caesars Cipher?
0

#1
function rot13(str) { // LBH QVQ VG!
  var arr = str.split('');
  function add13(someStr){
    if (someStr.charCodeAt() > 90 || someStr.charCodeAt() < 65) {
      return String.fromCharCode(someStr.charCodeAt() + 0);
    }
    else if ((someStr.charCodeAt() + 13) > 90) {
      return String.fromCharCode(someStr.charCodeAt() + 13 - 90 + 64);    
    } else {
      return String.fromCharCode(someStr.charCodeAt() + 13);
    } 
  }
  var newArr = arr.map(add13);
  var regExp = /-/gi;
  return newArr.join('').replace(regExp, " ");
}

// Change the inputs below to test
rot13("SERR CVMMN!");

Any advice on how to make it more readable, concise, clean, etc would be appreciated .


#2

I have blurred your solution to avoid someone stumbling upon the solution accidentally and seeing a finished solution.
To blur a solution you simply put [spoiler] on the line before the code and [/spoiler] on the line after the code you want to blur.


#3

To make it more concise, see if you can make it only have one of these:

String.fromCharCode(someStr.charCodeAt() 

You have the above code written three times and you have the following:

someStr.charCodeAt()

written five times in your code. Reducing repetitive code helps to make solutions more readable. Also, using descriptive variable names helps readability.

Also, I see you split the string into an array, which I assume is to make use of the map function. This is a fairly readable solution. However, instead of splitting, mapping, and joining, see if you can iterate through the original string itself to build a final string to return. This would make the solution more efficient.


#4

Whoops! Thanks for blurring that out, I didn’t realize.


#5

Thanks for the suggestions, I’ll work on trying to implement them.