+ 6
Help debug the code
The active field continues below the grid. Uncaught TypeError: Cannot read property '8' of undefined Line: 253 https://code.sololearn.com/Wvo0ah19cC3q/?ref=app
3 Answers
+ 5
Vasiliy
The entire canvas is active, if click anywhere on it the Game.prototype.tileAt() will be invoked. But clicking anywhere below the grid will cause an error because you are trying to access an element of a row that is not there or in other words, you are trying to access the property of an object that doesn't exist.
Using optional chaining operator(?.) should get rid of the Uncaught TypeError
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
254 return this.grid[row]?.[col];
https://code.sololearn.com/W85HDPUJ4QvP/#js
+ 4
ODLNT, Excellent! đđ
You are the best, I have been struggling with this for several days and no one responded.
I didn't even know about such a simple way to solve the problem.
But in principle, I also thought of inserting an exclusion condition here yesterday. âșïž
But I would like to make only the grid active.đ€
+ 2
Vasiliy
Optional Chaining Operator is a part of the ES11/ ES2020 version.
https://medium.com/better-programming/8-new-features-shipping-with-es2020-7a2721f710fb
Link above is to an article about some other new features you may find interesting.
As for making only the grid active, I must admit I'm stumped maybe some of the more brilliant community members can help with that.
Either way, I'm glad I was able to help some.
Keep coding!