Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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字符串参数未定义,无法访问.length属性_Javascript_String_Methods_Parameters - Fatal编程技术网

Javascript字符串参数未定义,无法访问.length属性

Javascript字符串参数未定义,无法访问.length属性,javascript,string,methods,parameters,Javascript,String,Methods,Parameters,所以我正在进行freecodecamp挑战,我需要解码CESAR密码。我创建了一个从主函数调用的辅助函数,用于解码字符串中的每个单词。我在第二段代码中遇到了问题,因为它不断给我一个错误,即设置为参数的字符串参数未定义,并且我无法访问长度。有人能解释一下发生了什么事吗 免责声明:我对编码是新手,在过去的30分钟里我一直在寻找这个问题的答案,但我找不到答案。我觉得这个修复应该简单易行,如果有人觉得这个问题多余,请提前道歉 代码如下: function rot13(str) { // LBH QVQ

所以我正在进行freecodecamp挑战,我需要解码CESAR密码。我创建了一个从主函数调用的辅助函数,用于解码字符串中的每个单词。我在第二段代码中遇到了问题,因为它不断给我一个错误,即设置为参数的字符串参数未定义,并且我无法访问长度。有人能解释一下发生了什么事吗

免责声明:我对编码是新手,在过去的30分钟里我一直在寻找这个问题的答案,但我找不到答案。我觉得这个修复应该简单易行,如果有人觉得这个问题多余,请提前道歉

代码如下:

function rot13(str) { // LBH QVQ VG!
  var stringArray = [];
  stringArray = str.split(" ");
  var value = stringArray.length;
  var decodedWords = [];
  var iCount = 0;

  while(iCount < value){
    decodedWords.push(decodeWord(stringArray[i]));
    iCount++;
  }

  return decodeWord("Confused!");

}

function decodeWord(word) {

    var decodedWord = "";

    for (i = 0; i < word.length; i++){

      var cipherVal = word.charCodeAt(i);
      var decodedVal = cipherVal;

      if( cipherVal >= 97 && cipherVal <= 109 || cipherVal >= 65 && cipherVal <= 77){
        decodedVal = cipherVal + 13;
      }

      else if(cipherVal >= 110 && cipherVal <= 122 || cipherVal >= 78 && 
 cipherVal <= 90){
        decodedVal = cipherVal - 13;
      }

      decodedWord += String.fromCharCode(decodedVal);
    }
    return decodedWord;
  }
函数rot13(str){//LBH QVQ VG!
var stringArray=[];
stringArray=str.split(“”);
var值=stringArray.length;
var decodedWords=[];
var-iCount=0;
while(i计数<值){
push(decodedword(stringArray[i]);
iCount++;
}
返回一个单词(“困惑!”);
}
函数解码字(字){
var decodedWord=“”;
for(i=0;i=97&&cipherVal=65&&cipherVal=110&&cipherVal=78&&

cipherVal在第一个函数中,循环中的
i
应该是
iCount

var iCount = 0;
while(iCount < value) {
    decodedWords.push(decodeWord(stringArray[iCount]));
    iCount++;
}

据我所知,你在这件事上犯了一个错误
decodedWords.push(decodedword(stringArray[i]);

换成

decodedWords.push(decodedword(stringArray[iCount]);


希望它能工作。如果有任何问题,请告诉我
stringArray[i]
i
从哪里来?
console.log(word)
,您会看到它未定义。需要将
i
更改为
iCount
(或反之亦然),那么在
stringArray
中提供一些项目怎么样?哦,我的天啊。我很尴尬哈哈。在遇到一些错误并进行故障排除后,我开始胡乱处理代码,忘记更改while循环中的count变量,所以当我修复其他错误时,我完全忽略了这一个。对此表示抱歉,谢谢谢谢你帮我。听起来好像我写代码太久了,需要休息一下……哈哈哈,这些东西一直在溜走!
return decodedWords.join(" ");