11:chap_num: 4
22:prev_link: 03_functions
33:next_link: 05_higher_order
4- :load_files: ["js/code/jaques_journal .js", "js/04_data.js"]
4+ :load_files: ["js/code/jacques_journal .js", "js/04_data.js"]
55
66= Data Structures: Objects and Arrays =
77
@@ -42,10 +42,10 @@ endif::tex_target[]
4242== The weresquirrel ==
4343
4444Every now and then, usually between eight and ten in the evening,
45- Jaques finds himself transforming into a small furry rodent with a
45+ Jacques finds himself transforming into a small furry rodent with a
4646bushy tail.
4747
48- On the one hand, Jaques is quite glad that he doesn't have classic
48+ On the one hand, Jacques is quite glad that he doesn't have classic
4949lycanthropy. Turning into a squirrel tends to cause fewer problems than
5050turning into a wolf. Instead of having to worry about accidentally
5151eating the neighbor (_that_ would be awkward), he worries about being
@@ -56,14 +56,14 @@ at night, and putting a few walnuts on the floor to keep himself busy.
5656
5757image::img/weresquirrel.svg[alt="The weresquirrel"]
5858
59- That takes care of the cat and oak problems. But Jaques still suffers
59+ That takes care of the cat and oak problems. But Jacques still suffers
6060from his condition. The irregular occurrences of the transformation
6161make him suspect that they might be triggered by something.
6262For a while, he believed that it only happened on days when he
6363had touched trees. So he stopped touching trees entirely, and even
6464avoided going near them. But the problem persisted anyway.
6565
66- Switching to a more scientific approach, Jaques intends to start
66+ Switching to a more scientific approach, Jacques intends to start
6767keeping a daily log of everything he did that day, and whether he
6868changed form. With this data he hopes to
6969narrow down the conditions that trigger the transformations.
@@ -218,7 +218,7 @@ array's elements.
218218Back to the weresquirrel. A set of daily log entries can be
219219represented as an array. But the entries do not consist of just a
220220number or a string—each entry needs to store a list of activities, and
221- a boolean value that indicates whether Jaques turned into a squirrel.
221+ a boolean value that indicates whether Jacques turned into a squirrel.
222222Ideally, we would like to group these values together into a
223223single value, and then put these grouped values into an array of log
224224entries.
@@ -311,7 +311,7 @@ with all their arms in a neat row, labeled with numbers.
311311
312312image::img/octopus-array.jpg[alt="Artist's representation of an array"]
313313
314- So we can represent Jaques ’ journal as an array of
314+ So we can represent Jacques ’ journal as an array of
315315objects:
316316
317317[source,javascript]
@@ -380,7 +380,7 @@ a different object, which initially contains the same properties as
380380`object1` but lives a separate life.
381381
382382(((== operator)))JavaScript's `==` operator, when comparing objects,
383- will return `true` only if both values are precisely the
383+ will return `true` only if both objects are precisely the
384384same value. Comparing different objects will
385385return `false`, even if they have identical contents.
386386There is no “deep” comparison operation built into
@@ -389,7 +389,7 @@ JavaScript, but it is possible to write it yourself.
389389
390390== The lycanthrope's log ==
391391
392- So Jaques starts up his JavaScript interpreter, and sets up the
392+ So Jacques starts up his JavaScript interpreter, and sets up the
393393environment he needs to keep his journal.
394394
395395// include_code
@@ -483,20 +483,18 @@ transformations.
483483
484484== Computing correlation ==
485485
486- We can represent a two-by-two table in JavaScript with a four-element array (`[76,
487- 9, 4, 1]`). We could also use other representations, such as an array
488- containing two two-element arrays (`[[76, 9], [4, 1]]`), or an object
489- with property names like `"11"` and `"01"`, but the flat array is
490- simple and makes the expressions that access the
491- table pleasantly short. We'll interpret the indices to the array as
492- two-bit binary numbers, where the first digit refers
493- to the squirrel variable and the second digit
494- refers to event variable.
495- For example, the binary number `10`
496- refers to the case where the event (say, "pizza") is true, but
497- Jaques didn't turn into a squirrel.
498- And since binary `10` is 2 in decimal notation, we will store this
499- value in the array at index 2.
486+ We can represent a two-by-two table in JavaScript with a four-element
487+ array (`[76, 9, 4, 1]`). We could also use other representations, such
488+ as an array containing two two-element arrays (`[[76, 9], [4, 1]]`),
489+ or an object with property names like `"11"` and `"01"`, but the flat
490+ array is simple and makes the expressions that access the table
491+ pleasantly short. We'll interpret the indices to the array as two-bit
492+ binary numbers, where the rightmost digit refers to the squirrel
493+ variable and the leftmost digit refers to event variable. For example,
494+ the binary number `10` refers to the case where the event (say,
495+ "pizza") is true, but Jacques didn't turn into a squirrel. And since
496+ binary `10` is 2 in decimal notation, we will store this value in the
497+ array at index 2.
500498
501499(((phi coefficient)))This is the function that computes the _ϕ_
502500coefficient from such an array:
@@ -525,11 +523,11 @@ We have to sum two fields from the table to get fields like n~1•~,
525523because the sums of rows or columns are not stored directly in our
526524data structure.
527525
528- Jaques kept his journal for three months. The resulting data set is
526+ Jacques kept his journal for three months. The resulting data set is
529527available in the coding sandbox for this chapter!!tex (`eloquentjavascript.net/code`)!!,
530528where it is stored
531529in the `JOURNAL` variable, and in a downloadable
532- http://eloquentjavascript.net/code/jaques_journal .js[file].
530+ http://eloquentjavascript.net/code/jacques_journal .js[file].
533531
534532To extract a two-by-two table for a specific event from this journal,
535533we must loop over all the entries and tally up how many times
@@ -721,19 +719,19 @@ console.log(phi(tableFor("peanut teeth", JOURNAL)));
721719----
722720
723721Well, that's unmistakable! The phenomenon occurs precisely when
724- Jaques eats peanuts and fails to brush his teeth. If only he weren't
722+ Jacques eats peanuts and fails to brush his teeth. If only he weren't
725723such a slob about dental hygiene, he'd have never even noticed his
726724affliction.
727725
728- Knowing this, Jaques simply stops eating peanuts
726+ Knowing this, Jacques simply stops eating peanuts
729727altogether, and finds that this completely puts an end to his
730728transformations.
731729
732- All is well with Jaques for a while. But a few years later, he loses his job
730+ All is well with Jacques for a while. But a few years later, he loses his job
733731and is eventually forced to take employment with a
734732circus, where he performs as _The Incredible Squirrelman_ by stuffing his
735733mouth with peanut butter before every show. One day, fed up with this
736- pitiful existence, Jaques fails to change back into his
734+ pitiful existence, Jacques fails to change back into his
737735human form, hops through a crack in the circus tent, and vanishes
738736into the forest. He is never seen again.
739737
@@ -746,7 +744,7 @@ start by introducing some generally useful array methods.
746744(((push method)))(((pop method)))(((shift method)))(((unshift
747745method)))We saw `push` and `pop`, which add and remove elements at the
748746end of an array, earlier in this chapter. The corresponding methods
749- for adding and removing things to the start of an array are called
747+ for adding and removing things at the start of an array are called
750748`unshift` and `shift`.
751749
752750[source,javascript]
@@ -926,7 +924,7 @@ argumentCounter("Straw man", "Tautology", "Ad hominem");
926924take any number of arguments, like `console.log`. These typically
927925loop over the values in their `arguments` object. They can be used to
928926create very pleasant interfaces. For example, remember how we created
929- the entries to Jaques ’ journal:
927+ the entries to Jacques ’ journal:
930928
931929[source,javascript]
932930----
@@ -1075,7 +1073,7 @@ Objects and arrays (which are a specific kind of object) provide ways to group s
10751073value. Conceptually, this allows us to put a bunch of related things
10761074in a bag and run around with the bag, instead of trying to wrap our
10771075arms around all of the individual things and trying to hold on to
1078- them seperately .
1076+ them separately .
10791077
10801078Most values in JavaScript have properties, the exceptions being `null`
10811079and `undefined`. Properties are accessed using `value.propName` or
@@ -1281,14 +1279,14 @@ ifdef::html_target[]
12811279----
12821280// Your code here.
12831281
1284- console.log(arrayToList([1, 2 ]));
1285- // → {value: 1 , rest: {value: 2 , rest: null}}
1286- console.log(listToArray(arrayToList([1, 2, 3 ])));
1287- // → [1, 2, 3 ]
1288- console.log(prepend(1 , prepend(2 , null)));
1289- // → {value: 1 , rest: {value: 2 , rest: null}}
1290- console.log(nth(arrayToList([1, 2, 3 ]), 1));
1291- // → 2
1282+ console.log(arrayToList([10, 20 ]));
1283+ // → {value: 10 , rest: {value: 20 , rest: null}}
1284+ console.log(listToArray(arrayToList([10, 20, 30 ])));
1285+ // → [10, 20, 30 ]
1286+ console.log(prepend(10 , prepend(20 , null)));
1287+ // → {value: 10 , rest: {value: 20 , rest: null}}
1288+ console.log(nth(arrayToList([10, 20, 30 ]), 1));
1289+ // → 20
12921290----
12931291endif::html_target[]
12941292
0 commit comments