How would I reference a dynamic local variable? This is easily accomplished with a global variable:
myPet = "dog";
console.log(window["myPet"]);
How would I do the same in a local scope?
Specifically what I'm trying to do:
myArray = [100,500,200,800];
a = 1; // Array index (operand 1)
b = 2; // Array index (operand 2)
Depending on the situation, I want to evaluate a<b or b<a
- To accomplish this, I set two variables: compare1 and compare2
- compare1 will reference either a or b and compare2 will reference the other
- Evaluate compare1 < compare2 or vice-versa
The following works perfectly with global variables. However, I want a and b to be local.
compare1 = "b"; compare2 = "a";
for(a=0; a<myArray.length; a++){
b = a+1;
while(b>=0 && myArray[window[compare1]] < myArray[[compare2]]){
/* Do something; */
b--;
}
}
If in the above I set compare1=a
then I would have to reset compare1 every time a changed. Instead, I want to actually [look at/point to] the value of a.