Skip to Content
Use ChatGPT to Continue the Conversation with High Scores
shortcut

Use ChatGPT to Continue the Conversation with High Scores

by J. Chris Anderson
November 2023
5 pages
3m
English
O'Reilly Media, Inc.
Content preview from Use ChatGPT to Continue the Conversation with High Scores

Use ChatGPT to Continue the Conversation with High Scores

Continuing from our previous tutorial (“Use ChatGPT to Extend the Generated App”), we will now add a list of high scores to the game. This is a bit more guided because I had an idea of what I was going for, now that the app is coming together:

Prompt: Create a high scores list that takes a list of scores as a prop, where each score is an object with { score, time} and time was created with Date.now().

ChatGPT: To create a high scores list component that takes a list of scores as a prop, follow these steps:

  1. Create a new file src/HighScores.js and add the following code:

    import React from 'react';
    import styled from '@emotion/styled';
    
    const HighScoresContainer = styled.div`
      margin-top: 20px;
    `;
    
    const HighScoreItem = styled.div`
      display: flex;
      justify-content: space-between;
      font-size: 18px;
      margin-bottom: 5px;
    `;
    
    const HighScores = ({ scores }) => {
      // Sort the scores array in descending order by score
      const sortedScores = scores.sort((a, b) => b.score -
        a.score);
    
      return (
        <HighScoresContainer>
          <h3>High Scores</h3>
          {sortedScores.map(({ score, time }, index) => {
            const date = new Date(time).toLocaleString();
            return (
              <HighScoreItem key={index}>
                <span>{score}</span>
                <span>{date}</span>
              </HighScoreItem>
            );
          })}
        </HighScoresContainer>
      );
    };
    
    export default HighScores;
  2. Import the HighScores component in src/App.js:

    import HighScores from './HighScores';
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Use ChatGPT to Take on the Role of a Coach in Turn-by-Turn Conversation

Use ChatGPT to Take on the Role of a Coach in Turn-by-Turn Conversation

J. Chris Anderson
What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz

Publisher Resources

ISBN: 9781098163105