Here is my Caesar Cipher solution - feedback please (spoilers)

Hello, i just completed the caesar cipher challenge and i want to get feedback.
here is my code.

`
    function rot13(str) { // LBH QVQ VG!
      var strArr = [];
      for (var i = 0; i < str.length; i++){
        if (/[a-z]/gi.test(str[i])){
          var charCode = str.charCodeAt(i) + 13;
          if (charCode > 90 || charCode < 65){
            charCode -= 26;
          }
          strArr.push(String.fromCharCode(charCode));
        }
        else {
          strArr.push(str[i]);
        }
      }
      str = strArr.join('');
      return str;
    }
    rot13("SERR CVMMN!");

`

This part is confusing:

  ...
var charCode = str.charCodeAt(i) + 13;
if (charCode > 90 || charCode < 65){
  charCode -= 26;
}
  ...

Firstly, I can’t see how charCode can be less than 65, but if in some strange way you could get it to be less than 65, your next line would make it even smaller.
So you can safely remove charCode < 65 from your if statement.

And this:

str = strArr.join('');
return str;

could be written in one line return strArr.join('');

Apart from that good job.

1 Like

thx for the comment, yea i found it meaningless now :smiley: