From the position in the matrix where 1 is placed I need to find the number of spaces (up, left, right, down) that you must move to reach 2. Also you can wrap around one side of the matrix to the other. So the example below should return 2, cause a 1 needs to move to the left first and then down. I am completely stuck.
var ARR = [
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 2],
[0, 0, 0, 0]
],
ONE, TWO;
for (x in ARR) {
for (y in ARR[x]) {
if (ARR[x][y] == 1) {
ONE = [x, y];
}
if (ARR[x][y] == 2) {
TWO = [x, y];
}
}
}
document.body.innerHTML = Math.abs(ONE[0] - TWO[0]) - Math.abs(ONE[1] - TWO[1]);