Skip to content

Commit 7aa1e60

Browse files
committed
Go over chapter 18
Make the merge more smooth, replace XMLHttpRequest with fetch, and modernize some of the text.
1 parent 7896bf8 commit 7aa1e60

File tree

7 files changed

+657
-1006
lines changed

7 files changed

+657
-1006
lines changed

06_object.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ console.log(okIterator.next());
664664

665665
{{index "matrix example", "Matrix class", array}}
666666

667+
{{id matrix}}
668+
667669
Let's implement an iterable data structure. We'll build a _matrix_
668670
class, acting as a two-dimensional array.
669671

18_http.md

Lines changed: 603 additions & 937 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const url = "https://eloquentjavascript.net/author";
2+
const types = ["text/plain",
3+
"text/html",
4+
"application/json",
5+
"application/rainbows+unicorns"];
6+
7+
async function showTypes() {
8+
for (let type of types) {
9+
let resp = await fetch(url, {headers: {accept: type}});
10+
console.log(`${type}: ${await resp.text()}\n`);
11+
}
12+
}
13+
14+
showTypes();

code/solutions/18_1_a_javascript_workbench.html renamed to code/solutions/18_2_a_javascript_workbench.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<pre id="output"></pre>
66

77
<script>
8-
document.querySelector("#button").addEventListener("click", function() {
9-
var code = document.querySelector("#code").value;
10-
var outputNode = document.querySelector("#output");
8+
document.querySelector("#button").addEventListener("click", () => {
9+
let code = document.querySelector("#code").value;
10+
let outputNode = document.querySelector("#output");
1111
try {
12-
var result = new Function(code)();
12+
let result = new Function(code)();
1313
outputNode.innerText = String(result);
1414
} catch (e) {
1515
outputNode.innerText = "Error: " + e;

code/solutions/18_2_autocompletion.html

Lines changed: 0 additions & 31 deletions
This file was deleted.

code/solutions/18_3_conways_game_of_life.html

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
<button id="run">Auto run</button>
66

77
<script>
8-
var width = 30, height = 15;
8+
const width = 30, height = 15;
99

1010
// I will represent the grid as an array of booleans.
1111

12-
var gridNode = document.querySelector("#grid");
12+
let gridNode = document.querySelector("#grid");
1313
// This holds the checkboxes that display the grid in the document.
14-
var checkboxes = [];
15-
for (var y = 0; y < height; y++) {
16-
for (var x = 0; x < width; x++) {
17-
var box = document.createElement("input");
14+
let checkboxes = [];
15+
for (let y = 0; y < height; y++) {
16+
for (let x = 0; x < width; x++) {
17+
let box = document.createElement("input");
1818
box.type = "checkbox";
1919
gridNode.appendChild(box);
2020
checkboxes.push(box);
@@ -23,15 +23,16 @@
2323
}
2424

2525
function gridFromCheckboxes() {
26-
return checkboxes.map(function(box) { return box.checked; });
26+
return checkboxes.map(box => box.checked);
2727
}
2828
function checkboxesFromGrid(grid) {
29-
return grid.forEach(function(value, i) { checkboxes[i].checked = value; });
29+
return grid.forEach((value, i) => checkboxes[i].checked = value);
3030
}
3131
function randomGrid() {
32-
var result = [];
33-
for (var i = 0; i < width * height; i++)
32+
let result = [];
33+
for (let i = 0; i < width * height; i++) {
3434
result.push(Math.random() < 0.3);
35+
}
3536
return result;
3637
}
3738

@@ -42,27 +43,29 @@
4243
// center field.
4344
function countNeighbors(grid, x, y) {
4445
var count = 0;
45-
for (var y1 = Math.max(0, y - 1); y1 <= Math.min(height - 1, y + 1); y1++) {
46-
for (var x1 = Math.max(0, x - 1); x1 <= Math.min(width - 1, x + 1); x1++) {
47-
if ((x1 != x || y1 != y) && grid[x1 + y1 * width])
48-
count += 1;
46+
for (let y1 = Math.max(0, y - 1); y1 <= Math.min(height - 1, y + 1); y1++) {
47+
for (let x1 = Math.max(0, x - 1); x1 <= Math.min(width - 1, x + 1); x1++) {
48+
if ((x1 != x || y1 != y) && grid[x1 + y1 * width]) {
49+
count++;
50+
}
4951
}
5052
}
5153
return count;
5254
}
5355

5456
function nextGeneration(grid) {
55-
var newGrid = new Array(width * height);
56-
for (var y = 0; y < height; y++) {
57-
for (var x = 0; x < width; x++) {
58-
var neighbors = countNeighbors(grid, x, y);
59-
var offset = x + y * width;
60-
if (neighbors < 2 || neighbors > 3)
57+
let newGrid = new Array(width * height);
58+
for (let y = 0; y < height; y++) {
59+
for (let x = 0; x < width; x++) {
60+
let neighbors = countNeighbors(grid, x, y);
61+
let offset = x + y * width;
62+
if (neighbors < 2 || neighbors > 3) {
6163
newGrid[offset] = false;
62-
else if (neighbors == 2)
64+
} else if (neighbors == 2) {
6365
newGrid[offset] = grid[offset];
64-
else
66+
} else {
6567
newGrid[offset] = true;
68+
}
6669
}
6770
}
6871
return newGrid;
@@ -74,8 +77,8 @@
7477

7578
document.querySelector("#next").addEventListener("click", turn);
7679

77-
var running = null;
78-
document.querySelector("#run").addEventListener("click", function() {
80+
let running = null;
81+
document.querySelector("#run").addEventListener("click", () => {
7982
if (running) {
8083
clearInterval(running);
8184
running = null;

html/example/submit.html

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33

44
<body style="font-family: arial">
55

6-
<p>You submitted:</p>
6+
<p>You submitted...</p>
77

8-
<dl></dl>
8+
<pre style="font: inherit"></pre>
99

1010
<script>
1111
function dec(str) {
1212
return decodeURIComponent(str.replace(/\+/g, " "));
1313
}
1414

15-
var dl = document.querySelector("dl");
16-
var params = document.location.search.slice(1).split("&");
17-
for (var i = 0; i < params.length; i++) {
18-
var param = params[i].split("=");
19-
var name = dec(param[0]), val = dec(param[1]);
20-
dl.appendChild(document.createElement("dt")).innerText = name;
21-
dl.appendChild(document.createElement("dd")).innerText = val || "(nothing)";
22-
}
15+
document.querySelector("pre").textContent =
16+
document.location.search.slice(1).split("&").map(param => {
17+
let [name, value] = param.split("=");
18+
return dec(name) + ": " + (dec(value) || "(nothing)");
19+
}).join("\n");
2320
</script>
2421

2522
</body>

0 commit comments

Comments
 (0)