Given the following array of arrays of numbers
numbers = [[7, 4, 0, 1], // see 0 at position [0][2]
[5, 6, 2, 2],
[6, 9, 7, 8],
[1, 4, 2, 0]]
I'd like to check if a number exists in inner array array[j]
and a array[j+2]
. array[j]
and array[j+2]
could be 0
at times. In an if statement like the below, I'm having problems due to 0
being a falsy value.
for (let i = 0; i < numbers.length; i++) {
for (let j = 0; j < numbers[0].length; j++) {
if (numbers[i][j] && numbers[i][j+2]) {
// how to evaluate this to true even if numbers[i][j] or numbers[i][j+2] is the number 0?
}
}
}
I tried adding an extra ||
check to see if that number was 0
, e.g. (numbers[i][j] || numbers[i][j] === 0) && (numbers[i][j+2] || numbers[i][j+2] === 0)
which I think looks terrible.