Skip to content

4. Create Variables to Hold Two Locations

Playback Speed:

In this video, you will begin to write computer code to calculate the distance between a housing option and a location that interests you, like school or work.

As it is now, you could make some basic calculations in your spreadsheet.

For example, you could calculate how much it would cost to rent a home for 8 or 12 months.

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.

But you also might like to know more personalized information, like the distance between your potential home and your school or workplace.

To do this, create your own spreadsheet function using code.

Think about a locations you might frequent, like a college you’d like to go to, or a relative’s house.

Add columns for these on your spreadsheet, and add the address.

From your spreadsheet, open the script editor.

Name your program “Driving Distance.”

The first variable in your code is your starting location, or origin.

To define it, type “VAR” 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.

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

On a new line, type “VAR, space, destination, space, equals.”

Then, 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.

In the next video, you’ll write code that communicates with the Google Maps service and gathers data about these two locations.

Now, it’s your turn: Add a column for another location of interest.

Open the script editor.

Define variables for origin and destination.

Then, type in the addresses surrounded by double quotation marks.

Save your code.

Run it to check for errors.

Then, move on to the next video.

Next arrow_forward
  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).
  • "Residential Lease Agreement with Pen" by Rental Realities ( -- Licensed by CC BY 2.0 ( -- Image scaled up cropping edges
  • " Bel Air Apartment Homes" by (