Skip to content

Commit 4dcc601

Browse files
committed
First pass over chapter 5
Different example data, use arrow functions, go into depth on UTF-16.
1 parent 5beb2f8 commit 4dcc601

17 files changed

+1747
-935
lines changed

01_values.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,14 @@ character is written like "\n"._” can be expressed:
288288
"A newline character is written like \"\\n\"."
289289
```
290290

291+
{{id unicode}}
291292
{{index [string, representation], Unicode, character}}
292293

293294
Strings, too, have to be modeled as a series of bits to be able to
294295
exist inside the computer. The way JavaScript does this is based on
295296
the _((Unicode))_ standard. This standard assigns a number to
296297
virtually every character you would ever need, including characters
297-
from Greek, Arabic, Japanese, Tamil, and so on. If we have a number
298+
from Greek, Arabic, Japanese, Armenian, and so on. If we have a number
298299
for every character, a string can be described by a sequence of
299300
numbers.
300301

02_program_structure.md

+2
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,8 @@ following triangle:
939939
#######
940940
```
941941

942+
{{index [string, length]}}
943+
942944
It may be useful to know that you can find the length of a string by
943945
writing `.length` after it.
944946

04_data.md

+31-12
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ The only step remaining is to find a correlation for every type of
652652
event that was recorded and see whether anything stands out. How
653653
should we store these correlations once we compute them?
654654

655+
{{id for_of_loop}}
656+
655657
## Array loops
656658

657659
{{index "for loop", loop, [array, iteration]}}
@@ -682,8 +684,8 @@ for (let entry of journal) {
682684

683685
When a `for` loops looks like this, with the word `of` after a
684686
variable definition, it will loop over the elements of the value given
685-
after `of`. This works not only for arrays, but also for some other
686-
data structures. We'll discuss _how_ it works in [Chapter
687+
after `of`. This works not only for arrays, but also for strings and
688+
some other data structures. We'll discuss _how_ it works in [Chapter
687689
6](06_object.html).
688690

689691
## Objects as maps
@@ -812,7 +814,7 @@ for (let event of Object.keys(correlations)) {
812814
// and so on...
813815
```
814816

815-
{{index "for/in loop"}}
817+
{{index "Object.keys function"}}
816818

817819
Most correlations seem to lie close to zero. Eating carrots, bread, or
818820
pudding apparently does not trigger squirrel-lycanthropy. It _does_
@@ -1037,7 +1039,7 @@ It can be useful for a function to accept any number of ((argument))s.
10371039
For example, `Math.max` computes the maximum of _all_ the arguments it
10381040
is given.
10391041

1040-
{{indexsee "period character", "max example"}}
1042+
{{indexsee "period character", "max example", spread}}
10411043

10421044
To write such a function, you put three dots before the function's
10431045
last ((parameter)), like this:
@@ -1059,6 +1061,21 @@ array containing all further arguments. If there are other parameters
10591061
before it, their values aren't part of that array. But if it's the
10601062
first parameter, as in `max`, it will hold all arguments.
10611063

1064+
{{index "function application"}}
1065+
1066+
You can use a similar three-dot notation to _call_ a function with an
1067+
array of arguments.
1068+
1069+
```
1070+
let numbers = [5, 1, 7];
1071+
console.log(max(...numbers));
1072+
// → 7
1073+
```
1074+
1075+
This "((spread))s" out the array into the function call, passing its
1076+
elements as separate arguments. It is possible to include an array
1077+
like that along with other arguments, as in `max(9, ...numbers, 2)`.
1078+
10621079
## The Math object
10631080

10641081
{{index "Math object", "Math.min function", "Math.max function", "Math.sqrt function", minimum, maximum, "square root"}}
@@ -1197,12 +1214,14 @@ function phi([n00, n01, n10, n11]) {
11971214
This also works for ((binding))s created with `let`, `var`, or
11981215
`const`. If you know the value you are binding is an array, you can
11991216
use ((square brackets)) to "look inside" of the value, binding its
1200-
content.
1217+
content. This works well with `Object.entries`, for example.
12011218

12021219
```
1203-
let [a, b, c] = [1, 2, 3];
1204-
console.log(b);
1205-
// → 2
1220+
for (let [name, value] in {x: 0, y: 1}) {
1221+
console.log(name, value);
1222+
}
1223+
// → x 0
1224+
// → y 1
12061225
```
12071226

12081227
{{index object, "curly braces"}}
@@ -1543,11 +1562,11 @@ compare properties only when _both_ arguments are objects. In all
15431562
other cases you can just immediately return the result of applying
15441563
`===`.
15451564

1546-
{{index "for/in loop", "in operator"}}
1565+
{{index "Object.keys method", "in operator"}}
15471566

1548-
Use a `for`/`in` loop to go over the properties. You need to test
1549-
whether both objects have the same set of property names and whether
1550-
those properties have identical values. The first test can be done by
1567+
Use `Object.keys` to go over the properties. You need to test whether
1568+
both objects have the same set of property names and whether those
1569+
properties have identical values. The first test can be done by
15511570
counting the properties in both objects and returning false if the
15521571
numbers of properties are different. If they're the same, then go over
15531572
the properties of one object, and for each of them, verify that the

0 commit comments

Comments
 (0)