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?

  • What if you used arrays of arrays? Dynamically accessing names is usually a sign of needing something else. – Andrew Li May 01 '17 at 02:55
  • You can wrap your arrays in an object and access its properties like `wrappedObject['Array' + row]` instead of using `eval`. – Saravana May 01 '17 at 02: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". – nnnnnn May 01 '17 at 03:02

2 Answers2

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");
    }
  }
}
Community
  • 1
  • 1
Hydrothermal
  • 4,851
  • 7
  • 26
  • 45
  • *"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 May 01 '17 at 03:03
  • @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. – Hydrothermal May 01 '17 at 03:09
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
        }
    }
}
Blender
  • 289,723
  • 53
  • 439
  • 496