1. The Weresquirrel
Every now and then, usually between 8 pm and 10 pm, Jacques finds himself transforming into a small furry rodent with a bushy tail.
On one hand, Jacques is quite glad that he doesn’t have classic lycan- thropy. Turning into a squirrel does cause fewer problems than turning into a wolf. Instead of having to worry about accidentally eating the neighbor (that would be awkward), he worries about being eaten by the neighbor’s cat. After two occasions where he woke up on a precariously thin branch in the crown of an oak, naked and disoriented, he has taken to locking the doors and windows of his room at night and putting a few walnuts on the floor to keep himself busy.
That takes care of the cat and tree problems. But Jacques would prefer to get rid of his condition entirely. The irregular occurrences of the transformation make him suspect that they might be triggered by something. For a while, he believed that it happened only on days when he had been near oak trees. But avoiding oak trees did not stop the problem.
Switching to a more scientific approach, Jacques has started keeping a daily log of everything he does on a given day and whether he changed form. With this data he hopes to narrow down the conditions that trigger the transformations.
The first thing he needs is a data structure to store this information.
2. Data Sets
To work with a chunk of digital data, we’ll first have to find a way to represent it in our machine’s memory. Say, for example, that we want to represent a collection of the numbers 2, 3, 5, 7, and 11.
We could get creative with strings—after all, strings can have any length, so we can put a lot of data into them—and use “2 3 5 7 11” as our representation. But this is awkward. You’d have to somehow extract the digits and convert them back to numbers to access them.
let listOfNumbers = [2, 3, 5, 7, 11];
// → 5
// → 2
console.log(listOfNumbers[2 – 1]);
// → 3
The notation for getting at the elements inside an array also uses square brackets. A pair of square brackets immediately after an expression, with another expression inside of them, will look up the element in the left-hand expression that corresponds to the index given by the expression in the brackets.
The first index of an array is zero, not one. So the first element is retrieved with listOfNumbers. Zero-based counting has a long tradition in technology and in certain ways makes a lot of sense, but it takes some getting used to. Think of the index as the amount of items to skip, counting from the start of the array.
We’ve seen a few suspicious-looking expressions like myString.length (to get the length of a string) and Math.max (the maximum function) in past chapters. These are expressions that access a property of some value. In the first case, we access the length property of the value in myString. In the second, we access the property named max in the Math object (which is a collection of mathematics-related constants and functions).
// → TypeError: null has no properties
So if you know that the property you are interested in is called color, you say value.color. If you want to extract the property named by the value held in the binding i, you say value[i]. Property names are strings. They can be any string, but the dot notation works only with names that look like valid binding names. So if you want to access a property named 2 or John Doe, you must use square brackets: value or value[“John Doe”].
The elements in an array are stored as the array’s properties, using numbers as property names. Because you can’t use the dot notation with numbers and usually want to use a binding that holds the index anyway, you have to use the bracket notation to get at them.
The length property of an array tells us how many elements it has. This property name is a valid binding name, and we know its name in advance, so to find the length of an array, you typically write array.length because that’s easier to write than array[“length”].
Both string and array objects contain, in addition to the length property, a number of properties that hold function values.
let doh = “Doh”;
// → function
// → DOH
Every string has a toUpperCase property. When called, it will return a copy of the string in which all letters have been converted to uppercase. There is also toLowerCase, going the other way.
Interestingly, even though the call to toUpperCase does not pass any arguments, the function somehow has access to the string “Doh”, the value whose property we called. How this works is described in “Methods” on page 98.
Properties that contain functions are generally called methods of the value they belong to, as in “toUpperCase is a method of a string.”
This example demonstrates two methods you can use to manipulate arrays:
let sequence = [1, 2, 3];
// → [1, 2, 3, 4, 5]
// → 5
// → [1, 2, 3, 4]
The push method adds values to the end of an array, and the pop method does the opposite, removing the last value in the array and returning it.
These somewhat silly names are the traditional terms for operations on a stack. A stack, in programming, is a data structure that allows you to push values into it and pop them out again in the opposite order so that the thing that was added last is removed first. These are common in programming—you might remember the function call stack from “The Call Stack” on page 46, which is an instance of the same idea.
No Starch Press; 3rd edition.