This file contains hidden or 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) => { | |
let results = []; | |
try { | |
for (let i=0; i<promises.length; i++) { | |
let last = (i === promises.length - 1); | |
promises[i].then(value => { | |
results[i] = value; | |
if (last) resolve(results); | |
}).catch(reject); |
This file contains hidden or 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
// my solution | |
async function locateScalpel(nest) { | |
let results = network(nest).map(async name => { | |
// if the name of the nest that was last known to hold the scalpel | |
// is the current nest then we have found the scalpel | |
// else, return null | |
if (name === await anyStorage(nest, name, 'scalpel')) { | |
return name; | |
} else { | |
return null; |
This file contains hidden or 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 {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", | |
"Marketplace-Post Office", "Marketplace-Shop", | |
"Marketplace-Town Hall", "Shop-Town Hall" |
This file contains hidden or 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
// regular expression: | |
// at the beginning of the number, optionally have '+' or '-' | |
// followed by a decimal or integer number: | |
// one or more digits optionally followed by a decimal point followed by zero or more digits, or | |
// a decimal point followed by one or more digits | |
// followed by (optionally) at the end of the number: | |
// 'e' followed by | |
// '+' or '-' (optional), followed by | |
// one or more digits | |
// everything is case-insensitive (i) so 'e' or 'E' are both acceptable |
This file contains hidden or 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.'"; | |
// ' at begining of string or | |
// ' after non-word character or | |
// ' before non-word character or | |
// ' at and of string | |
console.log(text.replace(/^'|(\W)'|'(\W)|'$\b/g, "$1\"$2")); | |
// → "I'm the cook," he said, "it's my job." |
This file contains hidden or 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
// the letters 'ca' followed by one of 'r' or 't' | |
verify(/ca[rt]/, | |
["my car", "bad cats"], | |
["camper", "high art"]); | |
// 'p' optionally followed by 'r' followed by 'op' | |
verify(/pr?op/, | |
["pop culture", "mad props"], | |
["plop"]); |
This file contains hidden or 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 hidden or 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"); | |
} | |
} |
This file contains hidden or 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 logSetElements(value1, value2, set) { | |
console.log('set[' + value1 + '] = ' + value2); | |
} | |
class PGroup { | |
constructor(pg = new Set()) { | |
this.group = pg; | |
} | |
add(item) { | |
let g = PGroup.from(this.group); |
This file contains hidden or 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 runRobot(state, robot, memory) { | |
for (let turn = 0; ; turn++) { | |
if (state.parcels.length == 0) { | |
return turn; | |
} | |
let action = robot(state, memory); | |
state = state.move(action.direction); | |
memory = action.memory; | |
} | |
} |