0

I am coming from Python, and am not entirely familiar with the 'proper' JS/TS way of doing things.

I am looping through the elements of a set, and I am pushing lists of some of the elements onto a 2D array.

let res: number[][];
for (let posElement of posSet) {
    if (negSet.has(-1*posElement)) {
        res.push([-1*posElement, 0, posElement]);
    }
}

I am getting the following error TypeError: Cannot read properties of undefined (reading 'push'). What am I doing wrong, where am I messing up the syntax?

6
  • You did not assign any value to res, and as a result its value is undefined. Something that will help you will be to use const everywhere you can - it'll help you catch errors easier Commented Jun 22, 2022 at 4:35
  • Alright, what are the necessary things I need to do in order to rectify that? It is an empty array that I want to pass things onto. As I said I am coming from Python, and this would be legal code there. Commented Jun 22, 2022 at 4:37
  • Assign a value to res so that it isn't undefined Commented Jun 22, 2022 at 4:37
  • It is empty, until I need it. What would I assign to it, if it is empty? Commented Jun 22, 2022 at 4:38
  • An empty array. Commented Jun 22, 2022 at 4:38

1 Answer 1

1

like this initialize

let res: number[][]=[];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.