It's interesting, StackOverflow will give you (after sufficient searching) an existing solution close to what you want. It's pretty certain to be accurate/working in the context of the question asked on SO. You as the "problem solver" just have to adapt the similar solution to your own problem.
ChatGPT will try to respond with exactly what you want, but with (multiple) errors that must be fixed.
I'll take the working example and adapt it myself.
There was a blog post on SO about this recently, they are banning accounts posting ChatGPT answers because they appear reasonable at first glance, but try and execute the code and it has syntax errors or is just plain wrong.