JS letter encryption program, is there any room for optimization?

question:
one English word is known. The English word contains only the uppercase letters "Amurz" and the lowercase letters "Amurz". Your task is to translate English words into cipher text. The rule of translation is to replace all letters with the third letter after it, and assume that the character an is followed by the character z and the character An is followed by the character Z. For example, zero will be translated into chur.
input and output requirements:
enter an English word, each of which is no more than 100 in length, and the word ends with the carriage return newline character"n". Output the password text of the corresponding string, accounting for one line.
Program running effect:
Sample 1:
zero
chur
Sample 2:
AZazbf
DCdcei

Code:
the general idea is: get the ASC code value of each English character, add 3 bits back, and deal with other things that are out of range

.
<body>
<input type="text" id="letter" />
<button id="result"></button>
<script>
window.onload = function() {
    var letter = document.getElementById("letter");
    var result = document.getElementById("result");
    var upperMin = 65; //asc
    var upperMax = 90; //asc
    var lowerMin = 97; //asc
    var lowerMax = 122; //asc

    result.onclick = function() {
        var s = [];
        var letterStr = letter.value;
        if(!/[a-zA-Z]/.test(letterStr)) {
            alert("");
            return;
        }
        for(var i=0; i<letterStr.length; iPP) {
            var _code = letterStr[i].charCodeAt(); //asc
            var _move = _code+3; //3

            //asc90  12223
            if((/[A-Z]/.test(letterStr[i])&&_move > upperMax) || (/[a-z]/.test(letterStr[i])&&_move > lowerMax)) {
                //asc23
                _move = _code - 23;
            }

            s.push(String.fromCharCode(_move));
        }

        alert(s.join(""))
    }
}
</script>
</body>

is there a better way to implement it than this?

Sep.08,2021

in addition to ASCII code value, the known range of your question is directly an array aquiz . Just sort out the rules and figure out how to take the value

.

is stored in the array, and the cyclic shift is fine

var arr = Array(52)
for (let i = 0; i < 26; iPP) {
  arr[i] = String.fromCharCode(i + 65)
  arr[i + 26] = String.fromCharCode(i + 97)
}

function encodeStr(s) {
  return s.replace(/[A-Za-z]/g, c => arr[(arr.indexOf(c) + 3) % 52])
}

each letter conversion result is fixed, (if it is for speed optimization), just write a table


const encrypt =(str)=>{
    const obj ={"a":"d","b":"e","c":"f","d":"g","e":"h","f":"i","g":"j","h":"k","i":"l","j":"m","k":"n","l":"o","m":"p","n":"q","o":"r","p":"s","q":"t","r":"u","s":"v","t":"w","u":"x","v":"y","w":"z","x":"a","y":"b","z":"c","A":"D","B":"E","C":"F","D":"G","E":"H","F":"I","G":"J","H":"K","I":"L","J":"M","K":"N","L":"O","M":"P","N":"Q","O":"R","P":"S","Q":"T","R":"U","S":"V","T":"W","U":"X","V":"Y","W":"Z","X":"A","Y":"B","Z":"C"}
    let res='';
    for(let i=0;i<str.length;iPP){
        res+= obj[str[i]]
    }
    return res;
}
    

Menu