2017-02-24 130 views
0
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"); 

我能够将原始代码翻译为其实际字词,但我无法将它们更改为新字符串上的正确字词来完成。有人可以请帮助。如何用新字符串替换旧字符串的值

+0

调用'使用String.fromCharCode()'没有做什么用的返回值是毫无意义的。 – Pointy

+0

我知道,但我想知道如何处理返回的值,所以我可以得到“免费代码营”的输出 –

回答

0

尝试这样的事情......

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"); 
+0

我仍然有麻烦,因为它说alteString没有定义 –

+0

你能发布你如何“重新使用它? – ShimSham

+0

@Tony becuase'alteredString'没有在代码中定义,只有'alteredStr'。将第3行的'var alteredStr =“”;'更改为'var alteredString =“”;'。 – Darren