I made a simple tetris game in class form. Something very strage is happening with one of my methods when I call Object.values(). Here is the method in question:
colision_bot() {
let i;
console.log(this.falling)
let val = Object.values(this.falling);
console.log(val)
for (i = 0; i < val.length; i++) {
if (val[0] == 24 || (this.board[val[0]++][val[1]] == 1 && this._check(val[0]++, val[1]))) { return true }
}
return false
}
The value called (this.falling) is element of the following array:
blocks = [
{ '1': [1, 4], '2': [2, 4], '3': [3, 4], '4': [4, 4] },
{ '1': [1, 4], '2': [2, 4], '3': [3, 4], '4': [3, 5] },
{ '1': [1, 5], '2': [2, 5], '3': [3, 4], '4': [3, 5] },
{ '1': [1, 4], '2': [2, 4], '3': [2, 5], '4': [3, 5] },
{ '1': [1, 5], '2': [2, 4], '3': [2, 5], '4': [3, 4] },
{ '1': [1, 4], '2': [1, 5], '3': [2, 4], '4': [2, 5] },
{ '1': [1, 4], '2': [2, 4], '3': [2, 5], '4': [3, 4] }
]
Object.values() is returning the first value as NaN and the rest as expected. The logs:
{1: Array(2), 2: Array(2), 3: Array(2), 4: Array(2)}
1: (2) [1, 4]
2: (2) [2, 4]
3: (2) [3, 4]
4: (2) [4, 4]
__proto__: Object
tetris.js:139
(4) [Array(2), Array(2), Array(2), Array(2)]
0: NaN
1: (2) [2, 4]
2: (2) [3, 4]
3: (2) [4, 4]
length: 4
SOLVED,
The issue was I forgot the first index as it was arrey of arreys. val[0]
should have been val[i][0]
. Thanks to everyone who replied.