4
4
5
5
import * as PJSON from "./pseudo_json.mjs"
6
6
import * as fs from "fs"
7
+ import * as path from "path"
7
8
import jszip from "jszip"
8
9
9
10
let output = [ ] , failed = false ;
10
11
11
- let allSolutions = fs . readdirSync ( "code/solutions/" ) . filter ( file => ! / ^ ( \. | 2 [ 0 1 2 ] ) / . test ( file ) ) ;
12
-
13
12
for ( let file of fs . readdirSync ( "." ) . sort ( ) ) {
14
13
let match = / ^ ( ( \d + ) .* ) .m d $ / . exec ( file ) , chapNum = match && match [ 2 ] ;
15
14
if ( ! match || chapNum == 22 ) continue ;
@@ -36,7 +35,6 @@ for (let file of fs.readdirSync(".").sort()) {
36
35
solution = fs . readFileSync ( "code/solutions/" + file , "utf8" ) ;
37
36
extra = / ^ \s * < ! d o c t y p e h t m l > \s * ( < b a s e .* \n ( < s c r i p t s r c = .* \n ) * ) ? / . exec ( solution ) ;
38
37
if ( extra ) solution = solution . slice ( extra [ 0 ] . length ) ;
39
- allSolutions . splice ( allSolutions . indexOf ( file ) , 1 ) ;
40
38
} catch ( e ) {
41
39
console . error ( "File " , file , " does not exist." , e ) ;
42
40
failed = true ;
@@ -74,6 +72,22 @@ for (let file of fs.readdirSync(".").sort()) {
74
72
}
75
73
76
74
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
+ } )
77
91
if ( chapter . number == 20 ) chapter . exercises = [
78
92
{ name : "Search tool" ,
79
93
file : "code/solutions/20_1_search_tool.mjs" ,
@@ -147,8 +161,10 @@ output.push({
147
161
]
148
162
} ) ;
149
163
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." ) ;
152
168
failed = true ;
153
169
}
154
170
0 commit comments