NodeJS v8.12.0-x64 FOR LOOP意外结果

时间:2018-11-09 13:36:33

标签: node.js for-loop

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

“ i” 更改为 z,omega或其他变量名称时,循环将按预期运行。我想念什么吗?我的计算机有问题吗?

/*
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( "" );
}

i 用作变量时,我只会得到以下输出:

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

1 个答案:

答案 0 :(得分:2)

您的所有函数均未正确声明i变量。由于var i = 0得到hoisted,因此该变量将由您的所有函数共享。

例如:

function encodeString( str ){
  encodedStr = "";
  for (i=0; i < str.length; i++){ ... }
}

正确的声明:

function encodeString( str ){
  let encodedStr = "";
  for (let i=0; i < str.length; i++){ ... }
}

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