1

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.

Daniel
  • 13
  • 4
  • 1
    Hello, can you gave a minimal reproductible example ? Without the Tetris part (board, check ...) ? – Marco Apr 19 '21 at 13:56
  • 3
    Please show the output of `console.log(JSON.stringify(this.falling, null, 2))` and `console.log(JSON.stringify(val, null, 2))`. – Heretic Monkey Apr 19 '21 at 13:56
  • Likely related: [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/q/4057440) – VLAZ Apr 19 '21 at 13:58
  • `let val = Object.values(this.falling);` followed by `this.board[val[0]++][val[1]]` does not make much sense. You expect `val` to be an array *of arrays*. So `val[0]++` will therefore try to increment an array and mangle it into a number in the process (*most likely* producing `NaN`). Similarly, trying to get the key `val[1]` will mangle the array into a string then try to fetch that property. – VLAZ Apr 19 '21 at 14:02

2 Answers2

1

The problem is val[0]++.
val[0] is an array.

let 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] }
];
let val = Object.values(blocks[0]);
console.log(val[0]);
val[0]++;
console.log(val[0]);

I don't know what you meant to do there, but ++ is certainly the wrong choice.

Siguza
  • 21,155
  • 6
  • 52
  • 89
0

A weird thing to keep in mind when usingconsole.log is that doesn't print deep copies or immutable copies. I.e. it's output can be manipulated after its been printed. E.g. try this snippet in chrome's dev console, and then try expanding the logged array; it'll say 0: (2) [null, 2] instead of 0: (2) [1, 2].

    let obj = [[1,2]];
    console.log(obj); // [Array(2)]
    obj[0][0] = null;

So my hunch is somewhere else in your code, your modifying this.falling[0].

Edit, @Siguza's answer points out where you're modifying this.falling while my answer explains why that would effect your earlier console.log.

junvar
  • 11,151
  • 2
  • 30
  • 46