0

I have successfully accessed different arrays and their elements using a for loop and the eval function as shown below:

var Array1 = [A,B,C,D];
var Array2 = [D,B,C,A];
var Array3 = [B,C,A,D];
var Array4 = [A,D,B,C];

for(var row = 1; row <=4; row++){
  for(var column = 0; column <=3; column++){
    if(row<4 && eval("Array" + row)[column] == eval("Array" + (row +1))[column]){
       console.log("Value of Array" + row + "column" + column + "is equal to" + "value of Array" + eval(row + 1) + "column" + column + "\n");
    }
  }
}

The question I have is, am I correctly using the eval function. If it is not the correct way to use the eval function how do I dynamically access different arrays in the for loop without using the eval function?

3
  • What if you used arrays of arrays? Dynamically accessing names is usually a sign of needing something else. Commented May 1, 2017 at 2:55
  • You can wrap your arrays in an object and access its properties like wrappedObject['Array' + row] instead of using eval. Commented May 1, 2017 at 2:57
  • 1
    "am I correctly using the eval function" - What do you mean by "correctly"? It's valid syntax and it works, so "yes". But it would be better (and easy!) to structure the code so that you don't need eval() at all, so "no". Commented May 1, 2017 at 3:02

2 Answers 2

1

Use of eval like this, although it might work, is a bad idea and makes it very easy to write dangerous code. Since eval will execute its argument regardless of what is actually passed, bugs that result in passing the wrong argument can have much more serious consequences than they would if you weren't using eval. The answers to this SO question offer some more insight. Instead consider using an object of arrays:

var arrays = {
    Array1: [A,B,C,D],
    Array2: [D,B,C,A],
    Array3: [B,C,A,D],
    Array4: [A,D,B,C]
}

for(var row = 1; row <=4; row++){
  for(var column = 0; column <=3; column++){
    if(row<4 && arrays["Array" + row][column] == arrays["Array" + (row + 1)][column]){
       console.log("Value of Array" + row + "column" + column + "is equal to" + "value of Array" + (row + 1) + "column" + column + "\n");
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

"Use of eval like this...makes it very easy to write dangerous code" - Why would it be dangerous? I agree it's not a good idea, but it's not really dangerous when used "like this".
@nnnnnn It's possible that the arguments passed to it may end up being something other than you expect due to a bug and result in unintentionally executing deformed code. Normally while a bug like that would probably just create an exception, with eval it can cause a wide variety of unexpected behavior. In this specific case it's probably not that bad, but the potential is certainly there.
1

I wouldn't say using eval like this is a good idea. eval is very rarely used because it's hard to debug and can be replaced with something easier to understand in most cases. It has valid use cases, but this isn't one of them.

Use an array of arrays instead:

var A = 2, B = 2, C = 3, D = 4;

var grid = [
    [A,B,C,D],
    [D,B,C,A],
    [B,C,A,D],
    [A,D,B,C]
]

for (var row = 0; row < grid.length; row++) {
    for (var column = 0; column < grid[0].length; column++) {
        if (row + 1 < grid.length && grid[row][column] === grid[row + 1][column]) {
            // they're equal
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.