Lesson №4 "Code Generation with ChatGPT". Part 3.

In this lesson, we will explore the possibility of programming with GPT, such as writing code for the game "Rock, Paper, Scissors" in JavaScript and integrating it into a React application.

Generating JavaScript code

To begin, we will ask ChatGPT to create the code for the "Rock, Paper, Scissors" game in JavaScript.

An example request might be: "Write the code for the 'Rock, Paper, Scissors' game in JavaScript."

ChatGPT will generate code that might look something like this:

Interestingly, we can copy and execute the generated code directly in the browser console, which is its own execution environment.

Interestingly, we can copy and execute the generated code directly in the browser console, which is its own execution environment.

We copy the function from the generated code, open the console by pressing F12, and paste it there:

Now we call a function and get a dialog window to choose between rock, paper, or scissors.

Once we have confirmed that the function works, let's now consider whether we can achieve a tie or a loss.

"Scissors won :)

Understanding the Generated Code

If there are any parts of the code or terms that you don't understand, you can ask ChatGPT and it will explain them. An example question would be: 'ChatGPT, how does the comparison function work in the generated code of the game?'

The answer might be something like this:"

Practical task

Objective: Try to use ChatGPT to write a game of "Tic Tac Toe" in JavaScript.

Instructions:

  1. Ask ChatGPT to write the code for the "Tic Tac Toe" game in JavaScript.

  2. Copy the generated code into an HTML file.

  3. Open it in a browser and try playing!

Example:

  1. Request: "Write code for the game of 'Tic Tac Toe' in JavaScript."

  2. Response:

  1. Insert this code into the HTML file:

  1. Open it in the browser.

Conclusion

In conclusion of the lesson, it can be said that ChatGPT can help bring ideas to life, create new projects, and improve existing ones. This can be a useful tool for anyone studying programming, regardless of their level of knowledge.

Last updated