Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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
Node.js NodeJS v8.12.0-x64用于循环意外结果_Node.js_For Loop - Fatal编程技术网

Node.js NodeJS v8.12.0-x64用于循环意外结果

Node.js NodeJS v8.12.0-x64用于循环意外结果,node.js,for-loop,Node.js,For Loop,“i”是保留变量吗?这似乎是个不太可能的问题。但是,当我在我的计算机上运行以下简单代码时,FOR循环中使用的迭代变量设置为“I”,在第一次执行循环后“I”返回为63 当“i”更改为z、ω或其他变量时,循环按预期运行。我错过什么了吗?这是我电脑的问题吗 /* This javascript code demonstrates a simple encode/decode of a string using a hash of possible values stored in a JSON

“i”是保留变量吗?这似乎是个不太可能的问题。但是,当我在我的计算机上运行以下简单代码时,FOR循环中使用的迭代变量设置为“I”,在第一次执行循环后“I”返回为63

“i”更改为z、ω或其他变量时,循环按预期运行。我错过什么了吗?这是我电脑的问题吗

/*
This javascript code demonstrates a simple encode/decode of a string using a hash of possible values
    stored in a JSON object.
*/

objPossibleCharacters = {
    "Z" : "c",          "Y" : "i",          "X" : "P",          "W" : "O",
    "V" : "b",          "U" : "5",          "T" : "V",          "S" : "a",
    "R" : "4",          "Q" : "Z",          "P" : "U",          "O" : "h",
    "N" : "d",          "M" : "R",          "L" : "3",          "K" : "N",
    "J" : "E",          "I" : "w",          "H" : "D",          "G" : "2",
    "F" : "e",          "E" : "v",          "D" : "q",          "C" : "j",
    "B" : "Q",          "A" : "1",          "0" : "G",          "1" : "f",
    "2" : "C",          "3" : "F",          "4" : "o",          "5" : "M",
    "6" : "0",          "7" : "r",          "8" : "L",          "9" : "H",
    "a" : "g",          "b" : "6",          "c" : "s",          "d" : "m",
    "e" : "S",          "f" : "7",          "g" : "x",          "h" : "p",
    "i" : "X",          "j" : "B",          "k" : "8",          "l" : "I",
    "m" : "y",          "n" : "T",          "o" : "k",          "p" : "J",
    "q" : "W",          "r" : "z",          "s" : "K",          "t" : "l",
    "u" : "t",          "v" : "9",          "w" : "A",          "x" : "Y",
    "y" : "u",          "z" : "n",          "!" : "%",          "?" : "~",
    "," : "*",          "." : "$",          " " : "|"
};

function encodeString( str ){
    encodedStr = "";
    for (i=0; i < str.length; i++){
        encodedStr += objPossibleCharacters[str.charAt(i)];
    }
    //console.log( encodedStr );
    return encodedStr;
}

function decodedString( str ){
    decodedStr = "";
    value = "";
    for ( i=0; i < str.length; i++ ){
        value = str.charAt(i);
        decodedStr += getKeyByValue( value );
    }
    //console.log( decodedStr );
    return decodedStr;
}

function getKeyByValue( value ){
    for ( key in objPossibleCharacters ){
        if ( objPossibleCharacters.hasOwnProperty( key ) ){

            if ( objPossibleCharacters[key] === value ) return key;
        }
    }
}

var strArray = [ 
    'Sea shells, sea shells.  She sells sea shells by the sea shore.', 
    'Thank you for coming today.  I hope you found this session enlightening and useful.', 
    'Elvis has left the building.  You do not need to go home, but you cannot stay here.  Get out!' ];

strArrayLength = strArray.length;

// This issue is in the following FOR LOOP
// Change var x = 0 to var i = 0
var x = 0;
for (x=0; x<strArrayLength; ++x){

        str = strArray[x];
        encodedOutput = encodeString( str );
        decodedOutput = decodedString( encodedOutput );

        console.log( "" );
        console.log( "Original string: " +str );
        console.log( "Encoded string: " +encodedOutput );
        console.log( "Decoded string: " +decodedOutput );
        console.log( strArrayLength +" - "+ x );
        console.log( "" );
}
当使用不同的变量名时,我得到以下输出:

Original string: Sea shells, sea shells.  She sells sea shells by the sea shore.
Encoded string: aSg|KpSIIK*|KSg|KpSIIK$||apS|KSIIK|KSg|KpSIIK|6u|lpS|KSg|KpkzS$
Decoded string: Sea shells, sea shells.  She sells sea shells by the sea shore.
3 - 63
Original string: Sea shells, sea shells.  She sells sea shells by the sea shore.
Encoded string: aSg|KpSIIK*|KSg|KpSIIK$||apS|KSIIK|KSg|KpSIIK|6u|lpS|KSg|KpkzS$
Decoded string: Sea shells, sea shells.  She sells sea shells by the sea shore.
3 - 0


Original string: Thank you for coming today.  I hope you found this session enlightening and useful.
Encoded string: VpgT8|ukt|7kz|skyXTx|lkmgu$||w|pkJS|ukt|7ktTm|lpXK|KSKKXkT|STIXxplSTXTx|gTm|tKS7tI$
Decoded string: Thank you for coming today.  I hope you found this session enlightening and useful.
3 - 1


Original string: Elvis has left the building.  You do not need to go home, but you cannot stay here.  Get out!
Encoded string: vI9XK|pgK|IS7l|lpS|6tXImXTx$||ikt|mk|Tkl|TSSm|lk|xk|pkyS*|6tl|ukt|sgTTkl|Klgu|pSzS$||2Sl|ktl%
Decoded string: Elvis has left the building.  You do not need to go home, but you cannot stay here.  Get out!
3 - 2

所有函数都没有正确声明
i
变量。因为
var i=0
get,所以该变量将被所有函数共享

例如:

function encodeString( str ){
  encodedStr = "";
  for (i=0; i < str.length; i++){ ... }
}
函数编码字符串(str){
编码器=”;
对于(i=0;i
适当声明:

function encodeString( str ){
  let encodedStr = "";
  for (let i=0; i < str.length; i++){ ... }
}
函数编码字符串(str){
让编码器“”;
对于(设i=0;i

您正在使用的其他变量也是如此。

所有函数都没有正确声明
i
变量。因为
var i=0
get,所以该变量将被所有函数共享

例如:

function encodeString( str ){
  encodedStr = "";
  for (i=0; i < str.length; i++){ ... }
}
函数编码字符串(str){
编码器=”;
对于(i=0;i
适当声明:

function encodeString( str ){
  let encodedStr = "";
  for (let i=0; i < str.length; i++){ ... }
}
函数编码字符串(str){
让编码器“”;
对于(设i=0;i

您正在使用的其他变量也是如此。

谢谢!我没有意识到可变提升。我在变量保持在其代码块范围内的范例下操作。提升变量上的链接产生以下结果:所有未声明的变量都是全局变量。我没有意识到这一点。我用“var”保留字在循环中声明了“I”,现在代码按预期运行。这是一堂关于正确声明变量的课。好的,Robert,我已经让代码正常工作了(在它们的范围内声明“I”)。然而,当我试图解决“63”来自何处时,我能得到的唯一逻辑答案是for(objPossibleCharacters中的键){…}。话虽如此,我的代码中并没有使用“i”变量。那么,for(…){…}返回“i”的基本逻辑是什么呢?如果是这样,我应该预料到吗?objPossibleCharacters对象中的“!”:“%”位于适当的位置,以生成此“63”结果,第三个字符串以“!”结尾。嗯…@EricG未声明的变量会产生意外的结果。这些结果可能是可以解释的,但老实说,这需要比我更好地分析代码:谢谢!我没有意识到可变提升。我在变量保持在其代码块范围内的范例下操作。提升变量上的链接产生以下结果:所有未声明的变量都是全局变量。我没有意识到这一点。我用“var”保留字在循环中声明了“I”,现在代码按预期运行。这是一堂关于正确声明变量的课。好的,Robert,我已经让代码正常工作了(在它们的范围内声明“I”)。然而,当我试图解决“63”来自何处时,我能得到的唯一逻辑答案是for(objPossibleCharacters中的键){…}。话虽如此,我的代码中并没有使用“i”变量。那么,for(…){…}返回“i”的基本逻辑是什么呢?如果是这样,我应该预料到吗?objPossibleCharacters对象中的“!”:“%”位于适当的位置,以生成此“63”结果,第三个字符串以“!”结尾。嗯…@EricG未声明的变量会产生意外的结果。这些结果可能是可以解释的,但老实说,这需要比我更好地分析代码:关于“被提升”变量的数据软件,请参见@robertklep response。我刚刚开始定期使用javascript编写代码,正确的变量声明比我想象的要重要一些。我刚刚开始定期使用javascript编写代码,正确的变量声明比我想象的要重要一些。