-1

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.

1
  • 1
    Please edit your question and insert a runnable snippet that produces the error message. You can use the toolbar in the editor to insert it. Commented Nov 26, 2023 at 15:01

1 Answer 1

-2

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];
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
I corrected the answer. The problem is with declaring the matrix.
@HovercraftFullOfEels Thank you for teaching me. I am new on this platform.
When I do that it doesn't even work the elements [0][n]. I tried this before write this questions. Another idea?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.