I'm trying to figure out how to find 3 or matching consecutive values in a 2D array. I think I have the horizontal and vertical matching figured out but how would I match if the same value was present in two different rows like this following list array (myArray):
| 1 | 2 | 2 | 0 |
| - | 2 | 0 | 1 |
| 1 | - | 0 | 1 |
In this particular 2D array, myArray[0][1], myArray[0][2], and myArray[1][1] all match the value of 2.
Here is the JavaScript I have to find matches for both horizontal and vertical matches:
for (i in myArray) {
for (j in myArray[i]) {
if (myArray[i][j] == myArray[i][j - 1] && myArray[i][j - 1] == myArray[i][j - 2] && myArray[i][j] != "-") {
// do something
}
if (i > 1) {
if (myArray[i][j] == myArray[i - 1][j] && myArray[i - 1][j] == myArray[i - 2][j] && myArray[i][j] != "-") {
// do something
}
}
}
}
Am I on the right track here? I'm guessing I can mix the two if statements to find the matches but I'm at a bit of a lost as to how.
Thanks!