Skip to content

Commit d3e85fb

Browse files
committed
Integrate reader suggestions in chapters 2-4
1 parent c836541 commit d3e85fb

File tree

4 files changed

+50
-43
lines changed

4 files changed

+50
-43
lines changed

02_program_structure.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ tentacles to it.
138138

139139
image::img/octopus.jpg[alt="Variables as tentacles"]
140140

141-
142141
An example. To remember the amount of dollars that Luigi still owes
143142
you, you create a variable. And then when he pays back 35 dollars, you
144143
give this variable a new value.
@@ -155,6 +154,16 @@ When you define a variable without giving it a value, the tentacle
155154
has nothing to grasp, so it ends in thin air. If you ask
156155
for the value of an empty variable, you'll get the value `undefined`.
157156

157+
A single `var` statement may define multiple variables. The
158+
definitions must be separated by commas.
159+
160+
[source,javascript]
161+
----
162+
var one = 1, two = 2;
163+
console.log(one + two);
164+
// → 3
165+
----
166+
158167
== Keywords and reserved words ==
159168

160169
(((keyword)))(((reserved words)))(((variable,naming)))Words with

03_functions.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,8 @@ A function may contain multiple `return` statements.
891891

892892
We've seen that `%` (the remainder operator) can be used to test
893893
whether a number is even or odd by using `% 2` to check if it's
894-
divisible by two. Here's another way to define whether a (positive,
895-
whole) number is even or odd:
894+
divisible by two. Here's another way to define whether a positive
895+
whole number is even or odd:
896896

897897
- Zero is even.
898898

04_data.txt

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

4444
Every 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
4646
bushy 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
4949
lycanthropy. Turning into a squirrel tends to cause fewer problems than
5050
turning into a wolf. Instead of having to worry about accidentally
5151
eating 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

5757
image::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
6060
from his condition. The irregular occurrences of the transformation
6161
make him suspect that they might be triggered by something.
6262
For a while, he believed that it only happened on days when he
6363
had touched trees. So he stopped touching trees entirely, and even
6464
avoided 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
6767
keeping a daily log of everything he did that day, and whether he
6868
changed form. With this data he hopes to
6969
narrow down the conditions that trigger the transformations.
@@ -218,7 +218,7 @@ array's elements.
218218
Back to the weresquirrel. A set of daily log entries can be
219219
represented as an array. But the entries do not consist of just a
220220
number 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.
222222
Ideally, we would like to group these values together into a
223223
single value, and then put these grouped values into an array of log
224224
entries.
@@ -311,7 +311,7 @@ with all their arms in a neat row, labeled with numbers.
311311

312312
image::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
315315
objects:
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
384384
same value. Comparing different objects will
385385
return `false`, even if they have identical contents.
386386
There 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
393393
environment 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 _ϕ_
502500
coefficient from such an array:
@@ -525,11 +523,11 @@ We have to sum two fields from the table to get fields like n~1•~,
525523
because the sums of rows or columns are not stored directly in our
526524
data 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
529527
available in the coding sandbox for this chapter!!tex (`eloquentjavascript.net/code`)!!,
530528
where it is stored
531529
in 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

534532
To extract a two-by-two table for a specific event from this journal,
535533
we 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

723721
Well, 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
725723
such a slob about dental hygiene, he'd have never even noticed his
726724
affliction.
727725

728-
Knowing this, Jaques simply stops eating peanuts
726+
Knowing this, Jacques simply stops eating peanuts
729727
altogether, and finds that this completely puts an end to his
730728
transformations.
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
733731
and is eventually forced to take employment with a
734732
circus, where he performs as _The Incredible Squirrelman_ by stuffing his
735733
mouth 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
737735
human form, hops through a crack in the circus tent, and vanishes
738736
into 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
747745
method)))We saw `push` and `pop`, which add and remove elements at the
748746
end 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");
926924
take any number of arguments, like `console.log`. These typically
927925
loop over the values in their `arguments` object. They can be used to
928926
create 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
10751073
value. Conceptually, this allows us to put a bunch of related things
10761074
in a bag and run around with the bag, instead of trying to wrap our
10771075
arms around all of the individual things and trying to hold on to
1078-
them seperately.
1076+
them separately.
10791077

10801078
Most values in JavaScript have properties, the exceptions being `null`
10811079
and `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
----
12931291
endif::html_target[]
12941292

0 commit comments

Comments
 (0)