When you ask ChatGPT to write a piece of code that does a thing, it is not reasoning about what the code should do, modeling the problem in its head, figuring out how to translate that into code, then writing it. The neural weights are spitting up a piece of code it determines would probably come after your prompt, based on similarity to a kajillion StackOverflow answers, GitHub repos, textbooks, etc etc. It is not coming up with a solution, just giving you the average answer to the million most similar questions.
ChatGPT can “write code” in the most literal sense that it will give you code when given a prompt, but (unlike how OpenAI markets-by-omission) it does not reason about code like an AGI could.