Selecting the Daily Prompt
Introduction: The Role of Daily Prompts in the Game
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!
Project File Structure Overview
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.
How the Game Chooses Today’s Prompt
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.
Loading and Validating Prompt Data
Selecting and Returning the Correct Prompt
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.
Step 1: Get the Day of the Year
- 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.
Step 2: Select the Prompt
Summary and What’s Next
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!
