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.
Follow the steps below to install the GitHub Copilot extension in Visual Studio or Visual Studio Code.
Install Visual Studio or Visual Studio Code if you don’t have it already.
Install GitHub Copilot by following the steps at https://docs.github.com/en/copilot/getting-started-with-github-copilot.
Install GitHub Copilot Chat by following the steps at https://docs.github.com/en/copilot/github-copilot-chat/using-github-copilot-chat.
NOTE: If you don’t currently have a GitHub Copilot license, you can sign up for a free trial.
Let’s get started with your first adventure!
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.
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.
Consider the sequence: [3, 6, 9, 12]
The next number echoed by the room should be: 15
This solution uses JavaScript and Node.js. Feel free to use another language/framework if you’d like.
Create a file named echo-chamber.js
in a folder of your choosing.
Add a const
named echoes
to hold the numeric sequence into the file:
const echoes = [3, 6, 9, 12];
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) {
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;
}
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));
Add the following comment immediately above the predictNext
function and press Enter.
// Document what this code is doing
Accept the suggestion to add a comment. Copilot should generate a comment similar to the following:
// This code is predicting the next number in the sequence by
// finding the difference between the first two numbers and
// adding that difference to the last number in the sequence.
NOTE: In addition to generating documentation, you can also use comments to generate code.
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 = [];
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
You should see code similar to the following generated:
memories.push(...echoes, next);
Save echo-chamber.js
and open a terminal window in the folder where you created the file.
Run the following command to execute the code. This assumes you have Node.js installed.
node echo-chamber.js
You should see 15
printed to the console which is the next number in the sequence.
See if you can get Copilot to output the memories to the console for you by adding a comment.
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.