Lecture 3 Activities

We’ll write a number of short procedures and library functions that are similar to how some of d3’s library is structured. This will work both as a test of your knowledge and familiarity with Javascript, and as a well to understand how you could have written much of d3 yourself.

Global variables

var objects = [{foo: 3, bar: "abc"},
               {foo: 5, bar: "def"}];

Functions

Exercises:

Objects and chaining

You will need to know the following: in order to iterate over fields of a JavaScript object1, use the following syntax:

for (var key in obj) {
    ...
}

Exercises:

Conversions

Putting it all together

Using the procedures above, write a small procedure that takes lists representing weather forecasts and return a new list of objects where the forecasts are in degrees Celsius.

  var forecasts = [
      { "city": "Washington, DC", "temperature": 92 },
      { "city": "New York", "temperature": 96 },
      { "city": "Seattle", "temperature": 77 },
      { "city": "Tucson", "temperature": 102 },
      { "city": "San Francisco", "temperature": 65 }
      ];

As we will see in the next lectures, these exercises really do have a lot to do with d3 and data visualization!

  1. If you know JavaScript, you know the story isn’t that simple. But then, you also know that this is close enough to get us to make progress.