Skip to content

Commit 63f9571

Browse files
committed
Add script includes to exercises that need them
1 parent c97b238 commit 63f9571

11 files changed

+39
-3
lines changed

14_event.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ ifdef::html_target[]
921921
height: 300px;
922922
}
923923
</style>
924+
924925
<script>
925926
// Your code here.
926927
</script>

bin/get_exercises.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ fs.readdirSync(".").forEach(function(file) {
3636
var type = sourceBlock[1].indexOf("html") > -1 ? "html" : "js";
3737
var file = chapNum + "_" + num + "_" + match[1].toLowerCase().replace(/[^\-\s\w]/g, "").replace(/\s/g, "_") + "." + type;
3838
try {
39-
var solution = fs.readFileSync("code/solutions/" + file, "utf8").trim();
39+
var solution = fs.readFileSync("code/solutions/" + file, "utf8");
40+
var extra = /^\s*<!doctype html>\s*(<base .*\n(<script src=.*\n)*)?/.exec(solution);
41+
if (extra) solution = solution.slice(extra[0].length);
4042
allSolutions.splice(allSolutions.indexOf(file), 1);
4143
} catch(e) {
42-
console.error("File ", file, " does not exist.");
44+
console.error("File ", file, " does not exist.", e);
4345
failed = true;
4446
}
4547
if (sourceBlock) {
@@ -49,7 +51,7 @@ fs.readdirSync(".").forEach(function(file) {
4951
number: num,
5052
type: type,
5153
code: sourceBlock[2],
52-
solution: solution
54+
solution: solution.trim()
5355
});
5456
break;
5557
}

code/solutions/13_1_build_a_table.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<!doctype html>
22

3+
<base href="http://eloquentjavascript.net/">
4+
<script src="code/mountains.js"></script>
5+
36
<style>
47
/* Defines a cleaner look for tables */
58
table { border-collapse: collapse; }
69
td, th { border: 1px solid black; padding: 3px 8px; }
710
th { text-align: left; }
811
</style>
912

13+
<body>
1014
<script>
1115
function buildTable(data) {
1216
var table = document.createElement("table");
@@ -37,3 +41,4 @@
3741

3842
document.body.appendChild(buildTable(MOUNTAINS));
3943
</script>
44+
</body>

code/solutions/13_3_the_cats_hat.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<!doctype html>
22

3+
<base href="http://eloquentjavascript.net/">
4+
35
<body style="min-height: 200px">
46

57
<img src="img/cat.png" id="cat" style="position: absolute">

code/solutions/14_2_mouse_trail.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
}
1313
</style>
1414

15+
<body>
1516
<script>
1617
var dots = [];
1718
for (var i = 0; i < 12; i++) {
@@ -29,3 +30,4 @@
2930
currentDot = (currentDot + 1) % dots.length;
3031
});
3132
</script>
33+
</body>

code/solutions/15_1_game_over.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<!doctype html>
22

3+
<base href="http://eloquentjavascript.net/">
4+
<script src="code/game_levels.js"></script>
5+
<script src="code/chapter/15_game.js"></script>
6+
37
<link rel="stylesheet" href="css/game.css">
48

9+
<body>
510
<script>
611
function runGame(plans, Display) {
712
function startLevel(n, lives) {
@@ -24,3 +29,4 @@
2429
}
2530
runGame(GAME_LEVELS, DOMDisplay);
2631
</script>
32+
</body>

code/solutions/15_2_pausing_the_game.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<!doctype html>
22

3+
<base href="http://eloquentjavascript.net/">
4+
<script src="code/game_levels.js"></script>
5+
<script src="code/chapter/15_game.js"></script>
6+
37
<link rel="stylesheet" href="css/game.css">
48

9+
<body>
510
<script>
611
// To know when to stop and restart the animation, a level that is
712
// being displayed may be in three states:
@@ -81,3 +86,4 @@
8186

8287
runGame(GAME_LEVELS, DOMDisplay);
8388
</script>
89+
</body>

code/solutions/16_2_the_pie_chart.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<!doctype html>
22

3+
<base href="http://eloquentjavascript.net/">
4+
<script src="code/chapter/16_canvas.js"></script>
5+
36
<canvas width="600" height="300"></canvas>
47
<script>
58
var cx = document.querySelector("canvas").getContext("2d");

code/solutions/19_1_rectangles.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<!doctype html>
22

3+
<base href="http://eloquentjavascript.net/">
4+
<script src="code/chapter/19_paint.js"></script>
5+
36
<script>
47
function rectangleFrom(a, b) {
58
return {left: Math.min(a.x, b.x),

code/solutions/19_2_color_picker.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<!doctype html>
22

3+
<base href="http://eloquentjavascript.net/">
4+
<script src="code/chapter/19_paint.js"></script>
5+
36
<script>
47
function colorAt(cx, x, y) {
58
var pixel = cx.getImageData(x, y, 1, 1).data;

0 commit comments

Comments
 (0)