This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note that it's skipping spaces to the next valid expression | |
function skipSpace(string) { | |
let spacesAndComments = string.match(/^(\s|#.*)*/); | |
return string.slice(spacesAndComments[0].length); | |
} | |
console.log(parse("# hello\nx")); | |
// → {type: "word", name: "x"} | |
console.log(parse("a # one\n # two\n()")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Represent Egg arrays with JavaScript arrays | |
topScope.array = (...elements) => { | |
return elements; | |
}; | |
topScope.length = (array) => { | |
return array.length; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Promise_all(promises) { | |
return new Promise((resolve, reject) => { | |
const array = []; | |
const promiseHelper = (promises) => { | |
if (promises.length === 0) { | |
resolve(array); // return array; | |
} | |
promises[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function locateScalpel(nest) { | |
let curr = nest.name; | |
let next = await anyStorage(nest, curr, "scalpel"); | |
while (next !== curr) { | |
curr = next; | |
next = await anyStorage(nest, curr, "scalpel"); | |
} | |
return curr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add dependencies and exports | |
(require, exports, module) => { | |
const {buildGraph} = require("./graph"); | |
const roads = [ | |
"Alice's House-Bob's House", "Alice's House-Cabin", | |
"Alice's House-Post Office", "Bob's House-Town Hall", | |
"Daria's House-Ernie's House", "Daria's House-Town Hall", | |
"Ernie's House-Grete's House", "Grete's House-Farm", | |
"Grete's House-Shop", "Marketplace-Farm", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill in this regular expression. | |
let number = /^[+-]?(\d+\.?\d*|\.\d+)(E[+-]?\d+)?$/i; | |
console.log(/\d/.test(".")); | |
// Tests: | |
for (let str of ["1", "-1", "+15", "1.55", ".5", "5.", | |
"1.3e2", "1E-4", "1e+12"]) { | |
if (!number.test(str)) { | |
console.log(`Failed to match '${str}'`); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let text = "'I'm the cook,' he said, 'it's my job.'"; | |
// Change this call. | |
console.log(text.replace(/(^|\W)'|'(\W|$)/g, '$1"$2')); | |
console.log(text.replace(/(^|\W)'/g, '$1"')); //Working as well due to the punctuation?? | |
// → "I'm the cook," he said, "it's my job." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill in the regular expressions | |
verify(/ca[rt]/, | |
["my car", "bad cats"], | |
["camper", "high art"]); | |
verify(/pr?op/, | |
["pop culture", "mad props"], | |
["plop", "prrrop"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const box = { | |
locked: true, | |
unlock() { this.locked = false; }, | |
lock() { this.locked = true; }, | |
_content: [], | |
get content() { | |
if (this.locked) throw new Error("Locked!"); | |
return this._content; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultiplicatorUnitFailure extends Error {} | |
function primitiveMultiply(a, b) { | |
if (Math.random() < 0.2) { | |
return a * b; | |
} else { | |
throw new MultiplicatorUnitFailure("Klunk"); | |
} | |
} |