CopilotAdventures

GitHub Copilot Warmup Adventure

GitHub Copilot is an AI pair programmer that helps you write better code. In this warm up adventure you’ll get to know Copilot and try it out for yourself. Here are a few things you can do with GitHub Copilot:

If you’re brand new to GitHub Copilot check out this video to learn more about the benefits it can bring to your development workflow. Once you’ve watched the video, come back here and choose an adventure to get started.

GitHub Copilot Video

Getting Started: Installing the GitHub Copilot Extension

Follow the steps below to install the GitHub Copilot extension in Visual Studio or Visual Studio Code.

  1. Install Visual Studio or Visual Studio Code if you don’t have it already.

  2. Install GitHub Copilot by following the steps at https://docs.github.com/en/copilot/getting-started-with-github-copilot.

What If I Don’t Have a GitHub Copilot License?

Are you a student? Learn more about how to setup and access GitHub Copilot here.

If you don’t currently have a GitHub Copilot license, you can:

Your First Adventure: The Chamber of Echoes

Let’s get started with your first adventure!

Background:

Perched atop the highest hill, overlooking the kingdom, stands the majestic Echo Castle. Within its stone walls is a chamber known as the Echo Room. This chamber, unlike any other, has the mystic power to echo numbers to anyone who enters. However, the Echo Room doesn’t just echo back any numbers; it always echoes the next number in a sequence.

Legends tell of a wizard who enchanted this chamber to test the intellect of visitors. The room would echo numbers in sequence, and only those who could predict the next echo were deemed worthy of the castle’s hidden treasures.

Objective:

Your task is to enter the Echo Room and listen to its sequence, then predict the next number it will echo. You have been provided with a series of numbers the room has echoed in the past. Use these numbers to determine the next one in the sequence.

Specifications:

  1. Input:
    • A list of at least three numbers that form a sequence. This list represents the numbers the room has echoed in the past.
  2. Output:
    • A single number representing the next number in the sequence.
  3. Assumptions:
    • The sequence will always be an arithmetic progression (The difference between consecutive numbers is constant).

Sample Data:

Consider the sequence: [3, 6, 9, 12]

The next number echoed by the room should be: 15

Constraints:

Summary of High-Level Tasks to Perform:

  1. Use a console application to render the output.
  2. Create a constant to hold the provided number sequence.
  3. Determine the common difference between consecutive numbers.
  4. Predict the next number in the sequence using the identified pattern.

GitHub Copilot Steps

This solution uses JavaScript and Node.js. Feel free to use another language/framework if you’d like.

  1. Create a file named echo-chamber.js in a folder of your choosing.

  2. Add a const named echoes to hold the numeric sequence into the file:

     const echoes = [3, 6, 9, 12];
    
  3. Type the following code after the echoes constant. Copilot should suggest code to help you determine the difference between consecutive numbers in the sequence.

     function predictNext(echoes) {
    
  4. To accept the suggestion, press Tab. The following function body should be added. Note that you may see a different code suggestion.

     function predictNext(echoes) {
         let difference = echoes[1] - echoes[0];
         let next = echoes[echoes.length - 1] + difference;
         return next;
     } 
    
  5. Enter a new line after the function and Copilot should suggest code similar to the following. If it doesn’t, begin typing console.log. To accept the suggestion, press Tab.

     console.log(predictNext(echoes));
    
  6. Move your cursor above the predictNext function and select CTRL + i (Windows) or CMD + i (Mac).

  7. Enter /doc into the textbox that appears and press Enter. You should see a comment generated for the function.

    inline chat in vs code

  8. Select Accept to accept the suggestion.

  9. You should see that a comment is generated for the function.

    NOTE: In addition to generating documentation, you can also use comments to generate code.

  10. Now let’s assume that you want to store the “memories” of the previous numbers echoed by the room. Add the following code after the echoes variable.

     const memories = [];
    
  11. Add the following comment immediately above the return next statement in the predictNext function and press Enter. Accept the suggestion by pressing Tab.

     // Store the full sequence including the predicted number in memories
    
  12. You should see code similar to the following generated:

     memories.push(...echoes, next);
    
  13. Save echo-chamber.js and open a terminal window in the folder where you created the file.

  14. Run the following command to execute the code. This assumes you have Node.js installed.

     node echo-chamber.js
    
  15. You should see 15 printed to the console which is the next number in the sequence.

  16. See if you can get Copilot to output the memories to the console for you by adding a comment.

Echo Castle’s Hidden Treasure is Now Yours!

You’ve completed your first GitHub Copilot “warmup” adventure! Check out the other adventures located in the Adventures folder to see other ways GitHub Copilot can be used.