Skip to content

Commit abff4fe

Browse files
committed
Fix some solutions in sandbox
1 parent e6741a3 commit abff4fe

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

code/index.html

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
</style>
1616
</head>
1717

18-
<div style="background: #ffb; border-bottom: 1px solid silver; padding: 6px 4em; text-align: center">
19-
<strong>Note:</strong> If you are reading the <strong>third edition</strong> of the book,
20-
you'll want to go
21-
to <a href="https://eloquentjavascript.net/3nd_edition/code/">that
22-
edition's sandbox</a> instead!
23-
</div>
24-
2518
<article style="max-width: 50em;">
2619
<h1>Code Sandbox<div style="font-size: 70%"><a class="subtlelink" href="../index.html">Eloquent JavaScript</a></div></h2>
2720

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let map = {one: true, two: true, hasOwnProperty: true};
2+
3+
console.log(Object.prototype.hasOwnProperty.call(map, "one"));
4+
// → true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
async function locateScalpel(nest) {
2+
let current = nest.name;
3+
for (;;) {
4+
let next = await anyStorage(nest, current, "scalpel");
5+
if (next == current) return current;
6+
current = next;
7+
}
8+
}
9+
10+
function locateScalpel2(nest) {
11+
function loop(current) {
12+
return anyStorage(nest, current, "scalpel").then(next => {
13+
if (next == current) return current;
14+
else return loop(next);
15+
});
16+
}
17+
return loop(nest.name);
18+
}
19+
20+
locateScalpel(bigOak).then(console.log);
21+
// → Butcher's Shop
22+
locateScalpel2(bigOak).then(console.log);
23+
// → Butcher's Shop

src/chapter_info.mjs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
import * as PJSON from "./pseudo_json.mjs"
66
import * as fs from "fs"
7+
import * as path from "path"
78
import jszip from "jszip"
89

910
let output = [], failed = false;
1011

11-
let allSolutions = fs.readdirSync("code/solutions/").filter(file => !/^(\.|2[012])/.test(file));
12-
1312
for (let file of fs.readdirSync(".").sort()) {
1413
let match = /^((\d+).*).md$/.exec(file), chapNum = match && match[2];
1514
if (!match || chapNum == 22) continue;
@@ -36,7 +35,6 @@ for (let file of fs.readdirSync(".").sort()) {
3635
solution = fs.readFileSync("code/solutions/" + file, "utf8");
3736
extra = /^\s*<!doctype html>\s*(<base .*\n(<script src=.*\n)*)?/.exec(solution);
3837
if (extra) solution = solution.slice(extra[0].length);
39-
allSolutions.splice(allSolutions.indexOf(file), 1);
4038
} catch(e) {
4139
console.error("File ", file, " does not exist.", e);
4240
failed = true;
@@ -74,6 +72,22 @@ for (let file of fs.readdirSync(".").sort()) {
7472
}
7573

7674
let nodeInfo = "// Node exercises can not be ran in the browser,\n// but you can look at their solution here.\n";
75+
if (chapter.number == 6) chapter.exercises.push({
76+
name: "Borrowing a method [3rd ed]",
77+
file: "code/solutions/06_4_borrowing_a_method.js",
78+
number: "4[3]",
79+
type: "js",
80+
code: "let map = {one: true, two: true, hasOwnProperty: true};\n\n// Fix this call\nconsole.log(map.hasOwnProperty(\"one\"));\n// → true",
81+
solution: "let map = {one: true, two: true, hasOwnProperty: true};\n\nconsole.log(Object.prototype.hasOwnProperty.call(map, \"one\"));\n// → true"
82+
})
83+
if (chapter.number == 11) chapter.exercises.push({
84+
name: "Tracking the scalpel [3rd ed]",
85+
file: "code/solutions/11_1_tracking_the_scalpel.js",
86+
number: "1[3]",
87+
type: "js",
88+
code: "async function locateScalpel(nest) {\n // Your code here.\n}\n\nfunction locateScalpel2(nest) {\n // Your code here.\n}\n\nlocateScalpel(bigOak).then(console.log);\n// → Butcher Shop",
89+
solution: "async function locateScalpel(nest) {\n let current = nest.name;\n for (;;) {\n let next = await anyStorage(nest, current, \"scalpel\");\n if (next == current) return current;\n current = next;\n }\n}\n\nfunction locateScalpel2(nest) {\n function loop(current) {\n return anyStorage(nest, current, \"scalpel\").then(next => {\n if (next == current) return current;\n else return loop(next);\n });\n }\n return loop(nest.name);\n}\n\nlocateScalpel(bigOak).then(console.log);\n// → Butcher's Shop\nlocateScalpel2(bigOak).then(console.log);\n// → Butcher's Shop"
90+
})
7791
if (chapter.number == 20) chapter.exercises = [
7892
{name: "Search tool",
7993
file: "code/solutions/20_1_search_tool.mjs",
@@ -147,8 +161,10 @@ output.push({
147161
]
148162
});
149163

150-
if (allSolutions.length) {
151-
console.error("Solution files " + allSolutions + " were not used.");
164+
let usedSolutions = new Set()
165+
for (let ch of output) for (let ex of ch.exercises) usedSolutions.add(path.basename(ex.file).replace(/\..*/, ""))
166+
for (let file of fs.readdirSync("code/solutions/")) if (!usedSolutions.has(file.replace(/\..*/, ""))) {
167+
console.error("Solution file " + file + " was not used.");
152168
failed = true;
153169
}
154170

src/client/code.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class CodeSandbox {
100100
if (chapter.start_code) code += "\n\n" + chapter.start_code
101101
this.setEditorState(code, {include: chapter.include})
102102
visible = "box"
103+
} else if (value == "11.1[3]") {
104+
document.location = "https://eloquentjavascript.net/3rd_edition/code/#11.1"
103105
} else {
104106
let exercise = findExercise(value, chapter)
105107
this.setEditorState(exercise.code, {
@@ -177,7 +179,7 @@ class CodeSandbox {
177179

178180
parseFragment() {
179181
let hash = document.location.hash.slice(1)
180-
let valid = /^(\d+)(?:\.(\d+))?$/.exec(hash)
182+
let valid = /^(\d+)(?:\.(\d+.*))?$/.exec(hash)
181183
let chapter, exercise
182184
if (valid) {
183185
chapter = getChapter(Number(valid[1]))
@@ -233,7 +235,7 @@ function findExercise(id, chapter) {
233235
let parts = id.split(".")
234236
if (!chapter) chapter = getChapter(parts[0])
235237
for (let i = 0; i < chapter.exercises.length; i++)
236-
if (chapter.exercises[i].number == +parts[1])
238+
if (chapter.exercises[i].number == parts[1])
237239
return chapter.exercises[i]
238240
}
239241

0 commit comments

Comments
 (0)