Welcome back! In the previous lesson, you learned how to generate and store prompt data for the LLM Prediction Game. Now, let’s see how to use that data to make the game dynamic and engaging for players.
A key part of the game is that each day, players see a new prompt. This keeps the game fresh and gives everyone the same challenge for that day. In this lesson, you’ll learn how to select the correct prompt for today using the data you’ve already created.
By the end of this lesson, you’ll understand how the game picks a prompt based on the current date, loads it from your JSON file, and handles any issues that might come up. You will use JavaScript’s Date object for date handling, and Node.js’s fs and path modules for file operations. This is an important step in making your game feel like a real daily challenge!
Before we dive into selecting the daily prompt, let’s get familiar with the main files and folders in your project. Here’s a quick overview of what each file does:
This structure helps keep your code organized and makes it easy to find where different parts of the game are implemented. In this lesson, we will work on the game.js file.
To make sure everyone gets the same prompt on a given day, we need a way to pick a prompt based on the date. One simple and effective method is to use the "day of the year." This means:
- January 1 is day 1
- February 1 might be day 32
- December 31 is day 365
Let’s see how you can get the day of the year in JavaScript.
If today is March 1, this code will print 60 (or 61 in a leap year). This number will help us pick which prompt to use from our list.
Why use the day of the year?
- It’s simple: just a number from 1 to 365.
- It repeats every year, so you can reuse prompts.
- It makes sure everyone playing on the same day gets the same prompt.
Now, let’s load the prompt data you created earlier. The prompts are stored in a JSON file, usually at static/prompts/data.json. We need to read this file and make sure it has the right number of prompts.
Here’s how you can load the prompts in Node.js:
Explanation:
- We use
path.jointo handle the file path in a cross-platform way. - We check if the file exists. If not, we throw an error.
- We read the file and parse its contents using
JSON.parse().
Once loaded, let’s also make sure the file has the correct number of prompts. For a daily game, you usually want 365 prompts (one for each day of the year):
This check helps catch mistakes early, such as missing or extra prompts.
Now that you have all the prompts loaded and validated, let’s select the prompt for today.
Remember that JavaScript arrays are zero-indexed. That means the first item is at index 0, the second at index 1, and so on. However, our day of the year starts at 1. So, we need to subtract 1 to get the correct index.
Let’s put it all together step by step.
- If no date is given, we use today’s date.
- We calculate the day of the year.
- We use
Math.min(day, 365)to make sure we don’t go past365, even in a leap year.
- We load and validate the prompts.
- We calculate the correct index for today’s prompt.
- We return the prompt for today.
Example Output:
Suppose today is January 2 (day 2 of the year). The function will return the second prompt in your list.
Output (example):
In this lesson, you learned how to select the correct daily prompt for your LLM Prediction Game:
- You used the day of the year to pick which prompt to show.
- You loaded and validated your prompt data from a
JSONfile. - You handled possible errors, such as missing files or the wrong number of prompts.
- You made sure to use the right index so the game always shows the correct prompt for today.
Next, you’ll get to practice these steps yourself. You’ll write code to select prompts, handle errors, and see how your game can deliver a new challenge every day. Good luck, and have fun!
