Skip to content

4. Create Variables to Hold Two Locations

Playback Speed:
Transcript

You could use your spreadsheet as it is now to make some basic calculations.

You could calculate the total cost of housing over an 8 or 12 month period.

You could divide the monthly rent by the number of people who will live in the home--you and two roommates, for example.

Or you could sort by cost and amenities, so that the least expensive home with a pool is at the top of your spreadsheet.

You also may want to perform more complicated calculations, like finding the distance between a rental property and your school or workplace.

To do this, you can write a function in the script editor.

You have already collected some addresses for rental properties.

In this video, you will program a new function to find the distance between each site and a location to which you would frequently travel.

Housing websites sometimes specify the distance between a home and a popular location, like a landmark or airport.

But these are not always locations that you would travel to most frequently.

Living near a friend, relative, or favorite restaurant might be more important to you.

Think about locations you might frequent, and add columns for them on your spreadsheet.

For example, if you plan to go to a certain college, enter the address here.

Or, if you want to live near your Aunt Beatrice, add her address in this column.

To write your program, you will practice hardcoding, or typing fixed parameters into the program.

In this case, you will type the physical address of each housing option directly into your code.

You will store these addresses as variables.

Variables allow you to label and store information that can be used throughout your program.

Once you have hardcoded the program and it works, you will change the code so that it automatically gets the data and outputs results to your spreadsheet.

So, why do both?

Hardcoding the addresses first makes it easier to test and find bugs in your program.

From your spreadsheet, open the script editor.

Name your program “Driving Distance.”

The first variable is your starting location, or origin.

To define it, type “V-A-R” for “variable,” a space, and “origin.”

Type an equals sign to assign the variable a value.

In this case, that’s the address you found for the first housing option.

Copy the address you found from your spreadsheet and paste it into the script editor with quotation marks around it.

The quotation marks indicate that the address is a string.

Next, add a variable for your destination--the place you are likely to visit often.

Type “V-A-R destination equals” and copy and paste the destination address from your spreadsheet into the code.

Make sure the address is inside quotation marks.

Save your project, then run it to check for errors.

No errors appear, but all the computer does so far is create two variables, or storage containers.

Check the values of your variables using “Logger dot log origin” and “Logger dot log destination.”

Check the log.

Great!

The origin and destination addresses appear.

When your log reads correctly, move on to the next video to write code that communicates with the Google Maps service and gathers data about these two locations.

Now, it’s your turn: Define variables for origin and destination.

Then, type in the addresses as strings.

Save your code.

Run it to check for errors.

Then, check the values of your variables using “logger dot log origin” and “logger dot log destination.”

Finally, move on to the next video.

Next
Instructions
  1. Define variables for origin and destination and type in the addresses as strings.
  2. Save your code.
  3. Run the code to check for errors.
  4. Check the values of your variables using Logger.log(origin) and Logger.log(destination).