My ugly solution for the Caesar Cipher assignment

function rot13(str) { // LBH QVQ VG!
  var encoded = [];
  var newStr = "";
  var newDigit = 90;
  str = str.toUpperCase();
  for (var i = 0; i < str.length; i++){
    if (str.charCodeAt(i) !== 32 && str.charCodeAt(i) !== 33 && str.charCodeAt(i) !== 63 && str.charCodeAt(i) !== 46){
      if(str.charCodeAt(i) >= 78){
        encoded.push(str.charCodeAt(i) - 13);
      } else{
          for (var j = 77; j > 52; j--){
            if(j === str.charCodeAt(i)){
              encoded.push(newDigit);
              newDigit = 90;
              break;
            }
            newDigit -= 1;
          }
        }
      } else {
        encoded.push(str.charCodeAt(i));
      }
    }
  for (var k = 0; k < encoded.length; k++){
    newStr = newStr + String.fromCharCode(encoded[k]);
  }
  
  return newStr;
}

// Change the inputs below to test
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");

Well your code works fine.
To be more concise, you better use some natives functions like charCodeAt() and fromCharCode() and avoid using break statements.
Here is an example :

function rot13(str) { // LBH QVQ VG!
var str_decipher = [];
var code = 0;
for (var i=0; i < str.length; i++) {

  code = str.charCodeAt(i);
  if (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0)) {
    code -= 13;
    if (code < 'A'.charCodeAt(0))
      code = 'Z'.charCodeAt(0) - ('A'.charCodeAt(0) - code - 1);
  }

  str_decipher.push(String.fromCharCode(code));
  console.log(str_decipher);
}

return str_decipher.join('');
}

// Change the inputs below to test
rot13("LBH QVQ VG!");
1 Like