+ 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

30th Nov 2020, 3:05 PM
Solo
Solo - avatar
3 Respostas
+ 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
1st Dec 2020, 6:24 PM
ODLNT
ODLNT - avatar
+ 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.🤔
1st Dec 2020, 8:09 PM
Solo
Solo - avatar
+ 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!
2nd Dec 2020, 12:02 PM
ODLNT
ODLNT - avatar