44
55import * as PJSON from "./pseudo_json.mjs"
66import * as fs from "fs"
7+ import * as path from "path"
78import jszip from "jszip"
89
910let output = [ ] , failed = false ;
1011
11- let allSolutions = fs . readdirSync ( "code/solutions/" ) . filter ( file => ! / ^ ( \. | 2 [ 0 1 2 ] ) / . test ( file ) ) ;
12-
1312for ( let file of fs . readdirSync ( "." ) . sort ( ) ) {
1413 let match = / ^ ( ( \d + ) .* ) .m d $ / . 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 * < ! 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 ) ;
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
0 commit comments