Build the Config Function
Transcript
In this video, you will write the Config part of your code.
If you do not know which part of the code you are responsible for, check with your group members.
Only watch the videos necessary to write your part of the code.
This function adds the movie poster URLs and the name of the movie poster to the website.
You will also write a function to help select a random poster.
To write the Config code, you will: create a posters variable that holds the names and URLs of each movie poster, write a function to select one of the posters randomly.
To start, select the "config.gs" file.
Then, create a variable to hold the name and URL of each poster.
Type "var, posters, equals open bracket." Then, press Enter several times to create a space for the URLs. Close the close the statement with a semicolon.
Within the posters variable, add an object to hold each poster name and URL.
Objects let you store related information together.
First, type an open curly brace, the letters URL, a colon, a space, two single quotation marks, and a comma.
Type a space, the word "name," a colon, a space, two single quotation marks.
Then, after the closed curly brace type a comma.
Then, copy and paste the line two more times so you have a total of three objects, one for each poster.
Remove the comma from the third line.
Next, get the exact URL for each movie poster.
To do this, go to Google Drive.
Right click to open a preview of the posters you created.
Hold the Control key and right-click the image.
Select "Open image in new tab." Copy the URL from the address bar and paste it into your code between the two single quotation marks.
Next, add a name for the poster object that identifies the poster's designer.
Make sure each name is unique and does not contain any punctuation.
This is the name that will identify the poster in your spreadsheet, so be sure to give each poster a recognizable name.
Repeat the same steps for each movie poster.
Preview the image.
Open it in a new tab.
Copy and paste the URL into the code, and give it a unique name.
Next, write a function to select one of the posters randomly.
Underneath the posters variable and after the last closed bracket, start a new line of code.
Type the following exactly as you see it on the screen.
Function, space, choosePosterNumber, open parentheses, open curly brace.
Then, press the Enter key.
The Render section of the code, which your partner is writing, will call this function.
Now, tell the computer to return a random whole number between zero and two.
Each number corresponds to one of the objects in the posters variable.
The number zero is the first poster, number one the second, and so on.
After the first curly brace, type "return, space, Math, dot." An autofill menu appears.
Select "round" from the menu.
This will make sure that the computer chooses a whole number instead of a decimal or a zero.
Add open and closed parentheses.
Inside the parentheses, type "math, dot" This time, select "random" from the autofill menu.
Add open and closed parentheses.
Then, type "space, asterisk, space two." Add a semicolon at the end of the line to close this statement.
Check to be sure your code looks exactly like what is on the screen.
Next, try running your program.
Save it, and click "Run." And accept the permissions.
Great! Next, run a test to ensure your code will work well with the other code files.
Tests are separate programs used to test sections of code.
Open "tests.gs" from the script editor sidebar.
Select the test posters function and run the code.
Then, check the code log.
See if the program returns any errors.
In this example, it does not.
If the log returns an error message like "You haven't created a variable named posters yet," go back to the Config file and check your code carefully.
If you need help, ask a neighbor or your teacher.
Once you have finished writing the Config.gs code, check on your group members and see if they need help.
Then, when you are all ready, move on to publish your group's code.
Now, it's your turn: Create a posters variable to hold the URL and name of your posters.
Add objects for all three posters in your variable, including name and URL.
Write a function to select one of the posters randomly.
And finally, test your program using the poster test function in the "test.gs" file.
Instructions
- Create a posters variable to hold the URL and name of your posters.
- Add objects for all three posters in your variable, including name and URL.
- Write a function to select one of the posters randomly.
- Test your program using the poster test function in the test.gs file.