Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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 使用JS在Caesars Cypher中循环_Javascript_Algorithm_Caesar Cipher - Fatal编程技术网

Javascript 使用JS在Caesars Cypher中循环

Javascript 使用JS在Caesars Cypher中循环,javascript,algorithm,caesar-cipher,Javascript,Algorithm,Caesar Cipher,我花了好几个小时试图自己解决这个问题,但在几个小时陷入绝境后,我得出了需要帮助的结论。我知道非常接近,但无法消除单词之间的空格。这是我的密码: function rot13(encodedStr) { var codeArr = encodedStr.split(""); var decodedArr = []; var letters = ["A", "B", "C", "D", "E", "F","G", "H", "I", "J", "K", "L", "M", "N", "

我花了好几个小时试图自己解决这个问题,但在几个小时陷入绝境后,我得出了需要帮助的结论。我知道非常接近,但无法消除单词之间的空格。这是我的密码:

function rot13(encodedStr) {
var codeArr = encodedStr.split("");
var decodedArr = []; 

var letters = ["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 space = " ";
var location = 0;
var temp = [];
for(var i = 0; i<codeArr.length; i++){

if(letters[i] !== " "){
location = letters.indexOf(codeArr[i]);
decodedArr.push(letters[(location+13)%26]);

}else

decodedArr.push(letters[i]);
}

return decodedArr.join(""); 
}

rot13("SERR CVMMN!");
功能rot13(编码器){
var codeArr=编码器拆分(“”);
var解码器=[];
变量字母=[“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 space=“”;
var位置=0;
var-temp=[];
对于(变量i=0;等轴测提示)
卫生设备和系统
让我们将所有字母标记为大写,以避免
rot13('SERR-CVMMN!');
rot13('SERR-CVMMN!');


接下来让我们来处理这个感叹号 应传递非字母字符。空格不是特例

if (/[A-Z]/.test(codeArr[i])) { // if the encoded character is an alphabet character
    // decode the character and push it on the decodedArr
} else {
    decodedArr.push(codeArr[i]); // let things like spaces and ! 
                                 // pass through to the output.
}

编码逻辑。 我认为你的逻辑有点倒退。与其在字母表上循环,为什么不在编码的文本上循环并按顺序解码呢

for (var i = 0; i < codeArr.length; i += 1) {
    if (/[A-Z]/.test(codeArr[i])) { // if the encoded character is an alphabet character
        // decode the character and push it on the decodedArr
        // the decoded character is 13 ahead of the current character in base 26.
        var ACode = 'A'.charCodeAt(); // 65
        var letter = codeArr[i];
        var letterCode = letter.charCodeAt() - ACode; // ASCII code
        letterCode = (letterCode + 13) % 26;
        decodedArr.push(letters[letterCode]);
    } else {
        decodedArr.push(codeArr[i]); // let things like spaces and ! 
                                     // pass through to the output.
    }
}
for(变量i=0;i
一些提示 卫生设备和系统 让我们将所有字母标记为大写,以避免
rot13('SERR-CVMMN!');
rot13('SERR-CVMMN!');


接下来让我们来处理这个感叹号 应传递非字母字符。空格不是特例

if (/[A-Z]/.test(codeArr[i])) { // if the encoded character is an alphabet character
    // decode the character and push it on the decodedArr
} else {
    decodedArr.push(codeArr[i]); // let things like spaces and ! 
                                 // pass through to the output.
}

编码逻辑。 我认为你的逻辑有点倒退。与其在字母表上循环,为什么不在编码的文本上循环并按顺序解码呢

for (var i = 0; i < codeArr.length; i += 1) {
    if (/[A-Z]/.test(codeArr[i])) { // if the encoded character is an alphabet character
        // decode the character and push it on the decodedArr
        // the decoded character is 13 ahead of the current character in base 26.
        var ACode = 'A'.charCodeAt(); // 65
        var letter = codeArr[i];
        var letterCode = letter.charCodeAt() - ACode; // ASCII code
        letterCode = (letterCode + 13) % 26;
        decodedArr.push(letters[letterCode]);
    } else {
        decodedArr.push(codeArr[i]); // let things like spaces and ! 
                                     // pass through to the output.
    }
}
for(变量i=0;i
谢谢你。至于落后的逻辑,我知道我做这件事的时间比我应该做的要长,但我是一个相当新的人,没有足够的知识可以依靠。我在这里学到了一些新东西,我非常感谢你。@sammyb123我很高兴我能帮上忙。你走上了正确的道路,做这样的练习非常有帮助。@sammyb123ul我确定。我试图设计一种思考问题的方式,我希望你能理解这个模式。我试图在将关注点粘在一起形成最终产品之前,将它们分开。干杯并感谢你接受答案。:d谢谢你。至于落后的逻辑,我知道我做这件事的时间比我应该做的要长,但我很抱歉我是新来的,我没有一个好的知识库可以依靠。我在这里学到了一些新东西,我真的很感谢你。@sammyb123我很高兴我能帮上忙。你走上了正确的轨道,做这样的练习肯定很有帮助。我试图设计一种思考问题的方式,我希望你能按照这个模式来思考。我试着在将这些问题粘在一起形成最终产品之前,先将它们从我的脑海中分离出来。干杯并感谢您接受答案。:D