Incredibly easy: ChatGPT programming for you

Incredibly easy: ChatGPT programming for you

About 2 years ago when ChatGPT became accessible to everyone, I immediately created an account out of curiosity about what lay behind the peculiar messages of this new toy. Initially, I amused myself by tormenting the chatbot with questions about the meaning of life, requesting a funny 10-line summary of Goethe's Faust 2, or asking for an age-appropriate bedtime story for my then 6-year-old granddaughter.

Then, I wanted to test the limits of the system and asked questions on topics ChatGPT didn't know the answer to: details about the rear axle of the 1978 Opel Kadett C or where exactly Charlemagne was buried in the Aachen Cathedral in 814. An interesting weakness emerged: when ChatGPT doesn't know, it shamelessly lies to you...

And because programming is my passion (currently for https://cam-ai.de), I also experimented with things like: "Write me a JavaScript program that plays Connect Four." The result was executable...

Nowadays, I regularly use ChatGPT in my programming work, mainly for these purposes:

  • When I don't have a solution for a problem yet and don't even know what to search for, I describe the problem and get a list of starting points for further investigation. Often, there are ideas that haven't occurred to me before.
  • When I have a simple little sub-problem, I delegate it to my digital colleague. It's important to provide a precise description of the task. Often, its result is still improvable but usually at least runnable. My effort is definitely reduced.
  • When my code isn't running as it should, ChatGPT increasingly finds the reason for it. I'd like to share an example with you:

Me: Here's an excerpt from my JS program:

if (isInViewport(checkelement)) {
  trigger_Socket.socket.send(result);
} else {
  setTimeout(trigger_Socket.socket.send(result), 5000);
};        

The delay of 5 seconds doesn't work like that. Why?

ChatGPT: The issue lies in how setTimeout is being used. setTimeout expects, as its first parameter, a function or a function expression to execute after the delay has elapsed. In your code, trigger_Socket.socket.send(result) is executed immediately and the result of this function is passed to setTimeout. That's not the desired behavior.

To achieve the delay, you need to use setTimeout with a function that calls trigger_Socket.socket.send(result). Here is a corrected version:

if (isInViewport(checkelement)) {
  trigger_Socket.socket.send(result);
} else {
  setTimeout(function() {
    trigger_Socket.socket.send(result);
  }, 5000);
};        

Done. I find that practical. What do you think?

要查看或添加评论,请登录

社区洞察

其他会员也浏览了