Skip to content

Commit 28efac4

Browse files
committed
Move exercises.html to code/index.html, also add project file downloads
1 parent a5fc3f4 commit 28efac4

File tree

12 files changed

+224
-134
lines changed

12 files changed

+224
-134
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/tex/*
22
/html/[012]*.html
3-
/html/js/exercise_data.js
3+
/html/js/chapter_info.js
44
/code/chapter/*
55
/code/file_server.js
66
/code/skillsharing.zip

02_program_structure.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ solve the exercise. If you run into problems, consider reading the
866866
hints (!html after the exercise!)(!tex at the link:solutions[end of the book]!).
867867
Full solutions to the exercises are not included in this
868868
book, but you can find them online at
869-
http://eloquentjavascript.net/exercises.html[_eloquentjavascript.net/exercises.html_].
869+
http://eloquentjavascript.net/code[_eloquentjavascript.net/code_].
870870
If you want to learn something from the exercises, I recommend looking
871871
at the solutions only after you've solved the exercise, or at least
872872
after you've attacked it long and hard enough to have a slight

04_data.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ because the sums of rows or columns are not stored directly in our
537537
data structure.
538538

539539
Jacques kept his journal for three months. The resulting data set is
540-
available in the coding sandbox for this chapter(!tex (http://eloquentjavascript.net/exercises.html[_eloquentjavascript.net/exercises.html_])!),
540+
available in the coding sandbox for this chapter(!tex (http://eloquentjavascript.net/code[_eloquentjavascript.net/code_])!),
541541
where it is stored
542542
in the `JOURNAL` variable, and in a downloadable
543543
http://eloquentjavascript.net/code/jacques_journal.js[file].
@@ -721,6 +721,8 @@ significant negative effect.
721721

722722
Interesting. Let's try something.
723723

724+
// include_code strip_log
725+
724726
[source,javascript]
725727
----
726728
for (var i = 0; i < JOURNAL.length; i++) {
@@ -937,7 +939,7 @@ argumentCounter("Straw man", "Tautology", "Ad hominem");
937939
// → You gave me 3 arguments.
938940
----
939941

940-
(((console.log function)))(((variadic function)))Some functions can
942+
(((console.log)))(((variadic function)))Some functions can
941943
take any number of arguments, like `console.log`. These typically
942944
loop over the values in their `arguments` object. They can be used to
943945
create very pleasant interfaces. For example, remember how we created

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ CHAPTERS := 00_intro 01_values 02_program_structure 03_functions 04_data 05_high
66

77
.SECONDARY: $(foreach CHAP,$(CHAPTERS),tex/$(CHAP).db)
88

9-
html: $(foreach CHAP,$(CHAPTERS),html/$(CHAP).html) html/js/exercise_data.js code/skillsharing.zip code/solutions/20_4_a_public_space_on_the_web.zip
9+
html: $(foreach CHAP,$(CHAPTERS),html/$(CHAP).html) html/js/chapter_info.js code/skillsharing.zip code/solutions/20_4_a_public_space_on_the_web.zip
1010

1111
html/%.html: %.txt asciidoc_html.conf
1212
PATH=node_modules/codemirror/bin:$(PATH) asciidoc -f asciidoc_html.conf --backend=html5 -o $@ $<
1313
node bin/build_code.js $<
1414

15-
html/js/exercise_data.js: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) code/solutions/*
16-
node bin/get_exercises.js > html/js/exercise_data.js
15+
html/js/chapter_info.js: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) code/solutions/* bin/chapter_info.js
16+
node bin/chapter_info.js > html/js/chapter_info.js
1717

1818
code/skillsharing.zip: html/21_skillsharing.html
1919
rm -f $@

bin/get_exercises.js renamed to bin/chapter_info.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ fs.readdirSync(".").forEach(function(file) {
1313
if (!match) return;
1414
var text = fs.readFileSync(file, "utf8");
1515
var exerciseSection = text.indexOf("\n== Exercises ==\n");
16-
if (exerciseSection < 0) return;
1716

1817
var chapter = {number: +chapNum,
1918
title: text.match(/\n= (.*?) =\n/)[1],
2019
exercises: []};
2120
var includes = text.match(/\n:load_files: (.*)/);
2221
if (includes) chapter.include = JSON.parse(includes[1]);
2322

24-
var exerciseBlock = text.slice(exerciseSection);
23+
var exerciseBlock = exerciseSection ? text.slice(exerciseSection) : "";
2524
var header = /\n=== (.*?) ===\n/g;
2625
var num = 1;
2726
while (match = header.exec(exerciseBlock)) {
@@ -59,46 +58,47 @@ fs.readdirSync(".").forEach(function(file) {
5958
++num;
6059
}
6160

62-
if (chapter.exercises.length) output.push(chapter);
63-
});
61+
var nodeInfo = "// Node exercises can not be ran in the browser,\n// but you can look at their solution here.\n";
62+
if (chapter.number == 20) chapter.exercises = [
63+
{name: "Content negotiation, again",
64+
file: "code/solutions/20_1_content_negotiation_again.js",
65+
number: 1,
66+
type: "js",
67+
code: nodeInfo,
68+
solution: fs.readFileSync("code/solutions/20_1_content_negotiation_again.js", "utf8")
69+
},
70+
{name: "Fixing a leak",
71+
file: "code/solutions/20_2_fixing_a_leak.js",
72+
number: 2,
73+
type: "js",
74+
code: nodeInfo,
75+
solution: fs.readFileSync("code/solutions/20_2_fixing_a_leak.js", "utf8")
76+
},
77+
{name: "Creating directories",
78+
file: "code/solutions/20_3_creating_directories.js",
79+
number: 3,
80+
type: "js",
81+
code: nodeInfo,
82+
solution: fs.readFileSync("code/solutions/20_3_creating_directories.js", "utf8")
83+
},
84+
{name: "A public space on the web",
85+
file: "code/solutions/20_4_a_public_space_on_the_web",
86+
number: 4,
87+
type: "js",
88+
code: nodeInfo,
89+
solution: "// This solutions consists of multiple files. Download it\n// though the link below.\n"
90+
}
91+
];
6492

65-
output.push({number: 20, title: "Node.js", exercises: [
66-
{name: "Content negotiation, again",
67-
file: "code/solutions/20_1_content_negotiation_again.js",
68-
number: 1,
69-
type: "js",
70-
code: "// Node exercises can not be ran in the browser",
71-
solution: fs.readFileSync("code/solutions/20_1_content_negotiation_again.js", "utf8")
72-
},
73-
{name: "Fixing a leak",
74-
file: "code/solutions/20_2_fixing_a_leak.js",
75-
number: 2,
76-
type: "js",
77-
code: "// Node exercises can not be ran in the browser",
78-
solution: fs.readFileSync("code/solutions/20_2_fixing_a_leak.js", "utf8")
79-
},
80-
{name: "Creating directories",
81-
file: "code/solutions/20_3_creating_directories.js",
82-
number: 3,
83-
type: "js",
84-
code: "// Node exercises can not be ran in the browser",
85-
solution: fs.readFileSync("code/solutions/20_3_creating_directories.js", "utf8")
86-
},
87-
{name: "A public space on the web",
88-
file: "code/solutions/20_4_a_public_space_on_the_web",
89-
number: 4,
90-
type: "js",
91-
code: "// Node exercises can not be ran in the browser\n\n// The solution for this exercise can be downloaded from:\n//\n// http://eloquentjavascript.net/code/solutions/20_4_a_public_space_on_the_web.zip",
92-
solution: "// Node exercises can not be ran in the browser"
93-
}
94-
]});
93+
output.push(chapter);
94+
});
9595

9696
if (allSolutions.length) {
9797
console.error("Solution files " + allSolutions + " were not used.");
9898
failed = true;
9999
}
100100

101101
if (!failed)
102-
console.log("var exerciseData = " + JSON.stringify(output, null, 2) + ";");
102+
console.log("var chapterData = " + JSON.stringify(output, null, 2) + ";");
103103
else
104104
process.exit(1);

code/ancestry.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/game_levels.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,6 @@ var GAME_LEVELS = [
135135
" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
136136
" "]
137137
];
138+
139+
if (typeof module != "undefined" && module.exports)
140+
module.exports = GAME_LEVELS;

html/exercises.html renamed to code/index.html

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<!doctype html>
22
<head>
33
<meta charset="utf-8">
4-
<title>Eloquent JavaScript :: exercises</title>
4+
<base href="..">
5+
<title>Eloquent JavaScript :: Code Sandbox</title>
56

67
<link rel=stylesheet href="js/node_modules/codemirror/lib/codemirror.css">
78
<script src="js/node_modules/codemirror/lib/codemirror.js"></script>
@@ -23,19 +24,21 @@
2324
<script src="js/node_modules/acorn/util/walk.js"></script>
2425
<link rel=stylesheet href="css/ejs.css">
2526
<script src="js/sandbox.js"></script>
26-
<script src="js/exercise_data.js"></script>
27-
<script src="js/exercises.js"></script>
27+
<script src="js/chapter_info.js"></script>
28+
<script src="js/code.js"></script>
2829
</head>
2930

3031
<article style="max-width: 50em;">
31-
<h1>Exercises<div style="font-size: 70%">Eloquent JavaScript</div></h2>
32+
<h1>Code Sandbox<div style="font-size: 70%">Eloquent JavaScript</div></h2>
3233

33-
<p>You can use this page to work on exercises when reading through a
34-
non-interactive version of Eloquent JavaScript, and to see solution
35-
code.</p>
34+
<p>You can use this page to download source code and solutions to
35+
exercises for the book Eloquent JavaScript, and to directly run code
36+
in the context of chapters from that book, either to solve exercises
37+
to to simply play around.</p>
3638

3739
<p>
38-
<select id="exercises"><option value="none">Select an exercise</option></select>
40+
Chapter: <select id="chapters"></select>
41+
<select id="per_chapter"></select>
3942
<button id="run">run code</button>
4043
</p>
4144

@@ -44,9 +47,19 @@ <h1>Exercises<div style="font-size: 70%">Eloquent JavaScript</div></h2>
4447
<div class="sandbox-output" id="output"></div>
4548
</div>
4649

47-
<p>If you've solved the exercise and want to compare your code with
50+
<div id="fileInfo">
51+
These files contain this chapter’s project code:
52+
<ul id="files" style="font-family: 'PT Mono', monospace; margin-top: 0"></ul>
53+
</div>
54+
55+
<p id="exercise_info">If you've solved the exercise and want to compare your code with
4856
mine, or you <em>really</em> tried, but can't get your code to work,
49-
you can <button id="solution" style="color: #c22">look at the
50-
solution</button> (or <a id="download" href="javascript:alert('No exercise
51-
selected')">download it</a>).</p>
57+
you can <button id="solution" style="color: #c22; font-size: 16px">look at the
58+
solution</button> (or <a id="download">download it</a>).</p>
59+
60+
<p id="box_info">
61+
The base environment for this chapter (if any) is available in the
62+
sandbox above, allowing you to run the chapter's examples by
63+
simply pasting them into the editor.
64+
</p>
5265
</article>

code/mountains.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ var MOUNTAINS = [
77
{name: "Denali", height: 6168, country: "United States"},
88
{name: "Popocatepetl", height: 5465, country: "Mexico"}
99
];
10+
11+
if (typeof module != "undefined" && module.exports)
12+
module.exports = MOUNTAINS;

html/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ <h3>(Part 3: Node)</h3>
7676
<h2>Other pages</h2>
7777

7878
<ul class="links">
79-
<li><a href="exercises.html">Exercise sandbox, with solutions</a></li>
79+
<li><a href="code">Code sandbox and exercise solutions</a></li>
8080
</ul>
8181

8282
</article>

0 commit comments

Comments
 (0)