JavaScript 中的凯撒密码

unl*_*cky 6 javascript caesar-cipher

我正在尝试编写一个程序来解决javascript中的以下问题(写在本段下面)。我不知道为什么我的代码不起作用。有人可以帮助我吗?我是 JavaScript 新手;这是一个免费的代码训练营问题。

\n\n

“现代常见的用法是 ROT13 密码,其中字母的值移动 13 位。因此 \'A\' \xe2\x86\x94 \'N\'、\'B\' \xe2\x86\ x94\'O\' 等等。

\n\n

编写一个函数,将 ROT13 编码字符串作为输入并返回解码后的字符串。”

\n\n

\r\n
\r\n
function rot13(str) { // LBH QVQ VG!\r\n  \r\n  var string = "";\r\n  for(var i = 0; i < str.length; i++) {\r\n    var temp = str.charAt(i);\r\n    if(temp !== " " || temp!== "!" || temp!== "?") {\r\n       string += String.fromCharCode(13 + String.prototype.charCodeAt(temp));\r\n    } else {\r\n      string += temp;\r\n    }\r\n  }\r\n  \r\n  return string;\r\n}\r\n\r\n// Change the inputs below to test\r\nconsole.log(rot13("SERR PBQR PNZC")); //should decode to "FREE CODE CAMP"
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

Mou*_*umi 11

使用模运算符;使句子大写;

function cipherRot13(str) {
  str = str.toUpperCase();
  return str.replace(/[A-Z]/g, rot13);

  function rot13(correspondance) {
    const charCode = correspondance.charCodeAt();
    //A = 65, Z = 90
    return String.fromCharCode(
            ((charCode + 13) <= 90) ? charCode + 13
                                    : (charCode + 13) % 90 + 64
           );
    
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我试图让它变得简单,我赢得了免费披萨!检查我的解决方案。

    function rot13(str) {
    
    var alphabets =['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'," ", "-", "_", ".", "&","?", "!", "@", "#", "/"];
    
    var alphabets13 = ['N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M', " ", "-", "_", ".", "&","?", "!", "@", "#", "/"];
    
    var resultStr = [];
    for(let i=0; i<str.length; i++){
        for(let j =0; j<alphabets.length; j++){
            if(str[i] === alphabets[j]){
            resultStr.push(alphabets13[j]);
            }
        }
    }
    return resultStr.join("");
  };

  rot13("SERR CVMMN!");
Run Code Online (Sandbox Code Playgroud)