Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用新字符串替换旧字符串的值 函数rot13(str){//LBH QVQ VG! var newStr=str.split(“”); 对于(变量i=0;i_Javascript_Arrays_Xcode_String - Fatal编程技术网

Javascript 如何用新字符串替换旧字符串的值 函数rot13(str){//LBH QVQ VG! var newStr=str.split(“”); 对于(变量i=0;i

Javascript 如何用新字符串替换旧字符串的值 函数rot13(str){//LBH QVQ VG! var newStr=str.split(“”); 对于(变量i=0;i,javascript,arrays,xcode,string,Javascript,Arrays,Xcode,String,我能够将原始代码翻译成它们的实际单词,但在完成新字符串时,我很难将它们更改为正确的单词。有人能帮忙吗。试试这样的 function rot13(str) { // LBH QVQ VG! var newStr = str.split(" "); for(var i = 0; i < newStr.length; i++ ){ for( var j = 0; j < newStr[i].length; j++ ){ if(newStr[i].charCodeAt

我能够将原始代码翻译成它们的实际单词,但在完成新字符串时,我很难将它们更改为正确的单词。有人能帮忙吗。

试试这样的

function rot13(str) { // LBH QVQ VG!
  var newStr = str.split(" ");

  for(var i = 0; i < newStr.length; i++ ){
    for( var j = 0; j < newStr[i].length; j++ ){
   if(newStr[i].charCodeAt(j) < 78){

     String.fromCharCode(newStr[i].charCodeAt(j) + 13);

   }
     else if(newStr[i].charCodeAt(j) >= 78){
          String.fromCharCode(newStr[i].charCodeAt(j) - 13);
          }
     }
  }
    return newStr;
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");
函数rot13(str){//LBH QVQ VG!
var newStr=str.split(“”);
var alteredStr=“”;
对于(var i=0;i=78{
//fromCharCode(newStr[i].charCodeAt(j)-13);
alteredString=alteredString+(newStr[i].charCodeAt(j)-13.toString();
}   
}   
}   
返回新闻TR;
}
//将下面的输入更改为测试
rot13(“SERR PBQR PNZC”);

调用
String.fromCharCode()
而不使用返回值是毫无意义的。我知道这一点,但我想知道如何处理返回值,以便获得“免费代码营”的输出我仍然有问题,因为它说alteredString没有定义。你可以发布你是如何使用它的吗?@Tony becuase
alteredString
没有在代码中定义,只有
alteredStr
有定义。变更
var alteredStr=“”第3行至
var alteredString=“”
function rot13(str) { // LBH QVQ VG!    
    var newStr = str.split(" ");   
    var alteredStr = "";

        for(var i = 0; i < newStr.length; i++ ){   
            for( var j = 0; j < newStr[i].length; j++ ){   
                if(newStr[i].charCodeAt(j) < 78){

                    //String.fromCharCode(newStr[i].charCodeAt(j) + 13);
                    alteredString = alteredString + (newStr[i].charCodeAt(j) + 13).toString();
                }   
                else if(newStr[i].charCodeAt(j) >= 78){   
                    //String.fromCharCode(newStr[i].charCodeAt(j) - 13);   
                    alteredString = alteredString + (newStr[i].charCodeAt(j) - 13).toString();   
                }   
            }   
        }   
    return newStr;
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");