The code is off but due to ambiguous prompt, and close enough it’s trivial to fix (the logic error is kinda an inhuman one though)
Are you using chatgpt 3.5? Found that chatgpt 4 has been a lot less error prone and codes much more efficiently