I want to asign values to a matrix each time that a conditions are comply.
I declarate it as let sortedPieces=[[]]
When is the time for asign values to the second row, debug console shows me this error: TypeError: Cannot set properties of undefined (setting '0')
An example of the result:
sortedPieces=[ [[1,2],[2,3],[3,4],[4,5]], [[1,2],[2,3],[3,4],[4,5]], [[1,2],[2,3],[3,4],[4,5]] ]
Input: sortedPieces[1][0]=[1,2]
Output: TypeError: Cannot set properties of undefined (setting '0')
Dont allow me to write the items (arrays) of the second row, I suspect that the problem is the matrix declaration.
-
1Please edit your question and insert a runnable snippet that produces the error message. You can use the toolbar in the editor to insert it.trincot– trincot2023-11-26 15:01:42 +00:00Commented Nov 26, 2023 at 15:01
Add a comment
|
1 Answer
The problem is from the declaration of the matrix
Please try this instead
let sortedPieces = [];
sortedPieces=[ [[1,2],[2,3],[3,4],[4,5]], [[1,2],[2,3],[3,4],[4,5]], [[1,2],[2,3],[3,4],[4,5]] ];
sortedPieces[1][0] = [1,2];
5 Comments
Hovercraft Full Of Eels
Please avoid code-dump only answers. While these sorts of answer might help the original poster (but only partially), they are completely useless for future visitors coming to the site with a similar problem, which is the purpose of the site. Please review the How to Answer link and then consider giving this answer an edit and an improvement.
trincot
Which change is the fix? You write that the initialisation is the problem, but the initialisation you do in the second line of the code is the same as the asker did.
BabaYaga
I corrected the answer. The problem is with declaring the matrix.
BabaYaga
@HovercraftFullOfEels Thank you for teaching me. I am new on this platform.
Ariadna Natsuki
When I do that it doesn't even work the elements [0][n]. I tried this before write this questions. Another idea?