Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Commit a563677

Browse files
committed
Pass over chapter 4
Lots of cleanup, try to make correlation formula less intimidating, add rest params, destructuring.
1 parent c52e5fa commit a563677

File tree

10 files changed

+692
-720
lines changed

10 files changed

+692
-720
lines changed

03_functions.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,11 @@ console.log(power(2, 6));
484484

485485
{{index "console.log"}}
486486

487-
In the [next chapter](04_data.html#arguments_object), we will see a
488-
way in which a function body can get at the exact list of arguments
489-
that were passed. This is helpful because it makes it possible for a
490-
function to accept any number of arguments. For example, `console.log`
491-
makes use of this—it outputs all of the values it is given.
487+
In the [next chapter](04_data.html#rest_parameters), we will see a way
488+
in which a function body can get at the whole list of arguments it was
489+
passed. This is helpful because it makes it possible for a function to
490+
accept any number of arguments. For example, `console.log` makes use
491+
of this—it outputs all of the values it is given.
492492

493493
```
494494
console.log("C", "O", 2);
@@ -1055,11 +1055,10 @@ hint}}
10551055

10561056
### Bean counting
10571057

1058-
{{index "bean counting (exercise)", "charAt method", [string, indexing], "zero-based counting"}}
1058+
{{index "bean counting (exercise)", [string, indexing], "zero-based counting"}}
10591059

10601060
You can get the Nth character, or letter, from a string by writing
1061-
`"string".charAt(N)`, similar to how you get its length with
1062-
`"s".length`. The returned value will be a string containing only one
1061+
`"string"[N]`. The returned value will be a string containing only one
10631062
character (for example, `"b"`). The first character has position zero,
10641063
which causes the last one to be found at position `string.length - 1`.
10651064
In other words, a two-character string has length 2, and its

04_data.md

Lines changed: 649 additions & 676 deletions
Large diffs are not rendered by default.

12_browser.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ HTML documents have a head and a body.
211211
The head contains information _about_ the document, and the body
212212
contains the document itself. In this case, we first declared that the
213213
title of this document is “My home page” and then gave a document
214-
containing a heading (`<h1>`, meaning “heading 1”—_<h2>_ to `<h6>`
214+
containing a heading (`<h1>`, meaning “heading 1”—_`<h2>`_ to `<h6>`
215215
produce more minor headings) and two ((paragraph))s (`<p>`).
216216

217217
{{index "href attribute", "a (HTML tag)"}}

code/jacques_journal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var JOURNAL = [
1+
const JOURNAL = [
22
{"events":["carrot","exercise","weekend"],"squirrel":false},
33
{"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false},
44
{"events":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false},
@@ -92,6 +92,6 @@ var JOURNAL = [
9292
];
9393

9494
// This makes sure the data is exported in node.js —
95-
// `require('./path/to/04_data.js')` will get you the array.
95+
// `require('./path/to/jaques_journal.js')` will get you the array.
9696
if (typeof module != "undefined" && module.exports)
9797
module.exports = JOURNAL;

code/solutions/03_3_bean_counting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function countChar(string, ch) {
22
var counted = 0;
33
for (var i = 0; i < string.length; i++) {
4-
if (string.charAt(i) == ch) {
4+
if (string[i] == ch) {
55
counted += 1;
66
}
77
}

code/solutions/04_1_the_sum_of_a_range.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
function range(start, end, step) {
2-
if (step == null) step = 1;
3-
var array = [];
1+
function range(start, end, step = start < end ? 1 : -1) {
2+
let array = [];
43

54
if (step > 0) {
6-
for (var i = start; i <= end; i += step)
7-
array.push(i);
5+
for (let i = start; i <= end; i += step) array.push(i);
86
} else {
9-
for (var i = start; i >= end; i += step)
10-
array.push(i);
7+
for (let i = start; i >= end; i += step) array.push(i);
118
}
129
return array;
1310
}
1411

1512
function sum(array) {
16-
var total = 0;
17-
for (var i = 0; i < array.length; i++)
13+
let total = 0;
14+
for (let i = 0; i < array.length; i++) {
1815
total += array[i];
16+
}
1917
return total;
2018
}
2119

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
function reverseArray(array) {
2-
var output = [];
3-
for (var i = array.length - 1; i >= 0; i--)
2+
let output = [];
3+
for (let i = array.length - 1; i >= 0; i--) {
44
output.push(array[i]);
5+
}
56
return output;
67
}
78

89
function reverseArrayInPlace(array) {
9-
for (var i = 0; i < Math.floor(array.length / 2); i++) {
10-
var old = array[i];
10+
for (let i = 0; i < Math.floor(array.length / 2); i++) {
11+
let old = array[i];
1112
array[i] = array[array.length - 1 - i];
1213
array[array.length - 1 - i] = old;
1314
}
@@ -16,7 +17,7 @@ function reverseArrayInPlace(array) {
1617

1718
console.log(reverseArray(["A", "B", "C"]));
1819
// → ["C", "B", "A"];
19-
var arrayValue = [1, 2, 3, 4, 5];
20+
let arrayValue = [1, 2, 3, 4, 5];
2021
reverseArrayInPlace(arrayValue);
2122
console.log(arrayValue);
2223
// → [5, 4, 3, 2, 1]

code/solutions/04_3_a_list.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
function arrayToList(array) {
2-
var list = null;
3-
for (var i = array.length - 1; i >= 0; i--)
2+
let list = null;
3+
for (let i = array.length - 1; i >= 0; i--) {
44
list = {value: array[i], rest: list};
5+
}
56
return list;
67
}
78

89
function listToArray(list) {
9-
var array = [];
10-
for (var node = list; node; node = node.rest)
10+
let array = [];
11+
for (let node = list; node; node = node.rest) {
1112
array.push(node.value);
13+
}
1214
return array;
1315
}
1416

1517
function prepend(value, list) {
16-
return {value: value, rest: list};
18+
return {value, rest};
1719
}
1820

1921
function nth(list, n) {
20-
if (!list)
21-
return undefined;
22-
else if (n == 0)
23-
return list.value;
24-
else
25-
return nth(list.rest, n - 1);
22+
if (!list) return undefined;
23+
else if (n == 0) return list.value;
24+
else return nth(list.rest, n - 1);
2625
}
2726

2827
console.log(arrayToList([10, 20]));

code/solutions/04_4_deep_comparison.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@ function deepEqual(a, b) {
22
if (a === b) return true;
33

44
if (a == null || typeof a != "object" ||
5-
b == null || typeof b != "object")
6-
return false;
5+
b == null || typeof b != "object") return false;
76

8-
var propsInA = 0, propsInB = 0;
7+
let propsInA = 0, propsInB = 0;
98

10-
for (var prop in a)
9+
for (let prop in a) {
1110
propsInA += 1;
11+
}
1212

13-
for (var prop in b) {
13+
for (let prop in b) {
1414
propsInB += 1;
15-
if (!(prop in a) || !deepEqual(a[prop], b[prop]))
15+
if (!(prop in a) || !deepEqual(a[prop], b[prop])) {
1616
return false;
17+
}
1718
}
1819

1920
return propsInA == propsInB;
2021
}
2122

22-
var obj = {here: {is: "an"}, object: 2};
23+
let obj = {here: {is: "an"}, object: 2};
2324
console.log(deepEqual(obj, obj));
2425
// → true
2526
console.log(deepEqual(obj, {here: 1, object: 2}));

src/run_tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let baseCode = "let alert = function() {}, prompt = function() { return 'x' }, c
1515
let meta = /\{\{meta (.*?)\}\}\n/.exec(input)
1616
let include = /\bload_files: (\[[^\]]+\])/.exec(meta ? meta[1] : "")
1717
if (include) JSON.parse(include[1]).forEach(function(fileName) {
18+
if (fileName == "code/chapter/" + file.replace(/md/, "js")) return
1819
let text = fs.readFileSync("html/" + fileName)
1920
if (!/\/\/ test: no/.test(text))
2021
baseCode += text

0 commit comments

Comments
 (0)