Why you should ALWAYS and THOROUGHLY review ChatGPT's code

TL;DR: sometimes ChatGPT does stupid coding mistakes. And if you ask it to correct them, sometimes it has no clue about the root cause and tries to hack around it, which is not what you want. 

 

Came across a fun issue when creating the second HTML page.

In the first page, a tiny bit of its JS code was stupidly wrong, and I didn't notice it:

It added an event listener to the button to proceed to the next page, and for mysterious reasons added a listener to add a listener, instead of adding a listener.

  startButton.addEventListener("click", function() {
      startButton.addEventListener("click", function() {
          ... do stuff ..
          // Navigate to the next screen, e.g.:
          window.location.href = "levelconfirmation.html";
      });
    });

As a result, the first click on the button wouldn't do anything, because the first event would just add the correct listener.

A really dumb mistake that shows the AI sometimes generates wild nonsense.

But the interesting bit came only then, when I asked ChatGPT what to do about this issue. Like LLM models do it too often, it started coming up with wild ideas how to fix it. Because it doesn't understand the code it has created in a way we do. So it told me:

I apologize for the issue you're experiencing. It seems that the problem might be related to how the event listeners are attached to the input fields. Since the input fields are created dynamically, the event listeners for updating the startButton.disabled property might not be attached properly when the page is first loaded. To fix this issue, you can modify the updateStartButtonState function and the event listeners in the scripts.js file as follows:

along with buggy code containing wrong html elements being used, additional event listeners being created.

All in all its reaction felt exactly like what a beginner who is over his head with an issue does; try some hacks to make the code work. In the end, with a number of iterations, maybe the code would have worked. But certainly it would have been a big mess that nobody understands, and not the code you want to work with.

So: closely review what the bot codes.

 

 

 

 

Kommentare

Beliebte Posts aus diesem Blog

ChatGPT as a teacher

How it started...

Pathfinding and A*