0

Javascript:

function decRot13(str) {

    var decode = '';

        for(var i = 0; i < str.length; i++){
            if(str.charCodeAt(i) + 13 > 90)
            //make calculations to reset from 65 to find new charCode value
                decode += String.fromCharCode(13 - (90 - str.charCodeAt(i)) + 64);
            else if(str.charCodeAt(i) + 13 <= 90 && str.charCodeAt(i) >= 65)    
              //if value is between 65 and 90 add 13 to charCode value
                decode += String.fromCharCode(str.charCodeAt(i) + 13);
            else if(str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90)
                //if value is less than 65 or greater than 90 add same value
                decode += String.fromCharCode(str.charCodeAt(i));
        }

        return decode;
    }

    function encRot13(str) {

    var encode = '';

        for(var i = 0; i < str.length; i++){

            if(str.charCodeAt(i) - 13 < 65)
                encode += String.fromCharCode(90 - (64 - (str.charCodeAt(i) - 13)));
            else if(str.charCodeAt(i) - 13 <= 90 && str.charCodeAt(i) >= 65)
                encode += String.fromCharCode(str.charCodeAt(i) - 13);
            else if(str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90)
                encode += String.fromCharCode(str.charCodeAt(i));
        }

        return encode;
    }

    alert(encRot13('LMNOP!'));

The decRot13(); function is producing the output I want, decrypts the cipher and ignores special characters like spaces and exclamation marks. (Essentially anything non-alphabetical).

I reversed the program but for some reason it produces the encrypted letters but alters the special characters (Which I want to be ignored.)

How do I fix this?

mincedMinx
  • 159
  • 1
  • 3
  • 12

0 Answers0