Reputation: 65
Is "i" a reserved variable? May seem like an unlikely question. However, when I run the following simple code on my computer and the iteration variable used in the FOR loop is set as "i", after the first execution of the loop "i" comes back as 63.
When "i" is changed to z, omega, or some other variable name the loop operates as expected. Am I missing something? Is this an issue with my computer?
/*
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( "" );
}
When i is used as the variable, I only get the following output:
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
When a different variable name is used, I get the following output:
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
Upvotes: 0
Views: 34
Reputation: 203304
All your functions don't properly declare the i
variable. Because var i = 0
gets hoisted, that variable will be shared by all of your functions.
For example:
function encodeString( str ){
encodedStr = "";
for (i=0; i < str.length; i++){ ... }
}
Proper declaration:
function encodeString( str ){
let encodedStr = "";
for (let i=0; i < str.length; i++){ ... }
}
The same goes for other variables that you're using too.
Upvotes: 2