What if you change the parameters inside the function, and then use return to return the parameters, but display the original input parameters?

clipboard.png
just a few weeks after contact with javascript, I encountered a question about Caesar"s password, so I started to write it, but I haven"t finished it yet. It is found that although the parameters have been changed inside the function, what should I do if I want to return the parameters that have been changed (to achieve the effect of letter shift), but return the original parameters as they are?

Mar.16,2021

str = str.replace...

as a matter of fact, there is no need to move back and forth between charCodeAt/fromCharCode. It can be handled completely and transferred to Unification

.
function rot13(str){
    //    Unicode
    var empty = Array.prototype.map.call(str,function(s){return s.charCodeAt(0)})
    // 
    .map(function(num){
        // left
        if(num >=65 && num <= 77){
            return num + 13;
        // right
        } else if(num >= 78 && num <= 90){
            return num;
        // 
        } else {
            return num;
        }
    });
    //  Unicode 
    return String.fromCharCode.apply(null, empty);
}
After

str performs replace, you need to assign the obtained content back to str


    In line 9 of
  1. code, do not use in , in to determine whether an object has an attribute, and not to judge whether an array contains an element. Use includes instead;
  2. Line 10 of
  3. code uses str = str.replace to reassign str ;
function rot13 (str) {
  var empty = [];
  for (var i = 0; i < str.length; iPP) {
    empty.push(str.charCodeAt(i))
  }
  var left = empty.filter(function (x) {return x >= 65 && x <= 77})
  var right = empty.filter(function (y) {return y >= 78 && y <= 90})
  for (var j = 0; j < str.length; jPP) {
    if (left.includes(str.charCodeAt(j))) { // includes
      str = str.replace(str[j], String.fromCharCode(str.charCodeAt(j) + 13)) // 
    }
  }
  return str
}
Menu