Skip to content

Instantly share code, notes, and snippets.

View jonurry's full-sized avatar
🎯
Product Developer at AND Digital, Node, TypeScript, AWS, always learning.

Jon Urry jonurry

🎯
Product Developer at AND Digital, Node, TypeScript, AWS, always learning.
View GitHub Profile
@jonurry
jonurry / 11-2 Eloquent Javascript Solutions.js
Created March 22, 2018 12:03
11.2 Building Promise.all() (Eloquent JavaScript Solutions)
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);
@jonurry
jonurry / 11-1 Eloquent Javascript Solutions.js
Created March 22, 2018 10:43
11.1 Tracking The Scalpel (Eloquent JavaScript Solutions)
// 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;
@jonurry
jonurry / 10-2 Eloquent Javascript Solutions.js
Created March 20, 2018 12:26
10.2 Roads Module (Eloquent JavaScript Solutions)
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"
@jonurry
jonurry / 9-3 Eloquent Javascript Solutions.js
Created March 20, 2018 10:20
9.3 Numbers Again (Eloquent JavaScript Solutions)
// 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
@jonurry
jonurry / 9-2 Eloquent Javascript Solutions.js
Created March 20, 2018 10:07
9.2 Quoting style (Eloquent JavaScript Solutions)
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."
@jonurry
jonurry / 9-1 Eloquent Javascript Solutions.js
Created March 19, 2018 18:26
9.1 RegExp Golf (Eloquent JavaScript Solutions)
// 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"]);
@jonurry
jonurry / 8-2 Eloquent Javascript Solutions.js
Last active March 15, 2018 16:40
8.2 The Locked Box (Eloquent JavaScript Solutions)
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;
}
};
@jonurry
jonurry / 8-1 Eloquent Javascript Solutions.js
Created March 15, 2018 16:03
8.1 Retry (Eloquent JavaScript Solutions)
class MultiplicatorUnitFailure extends Error {}
function primitiveMultiply(a, b) {
if (Math.random() < 0.2) {
return a * b;
} else {
throw new MultiplicatorUnitFailure("Klunk");
}
}
@jonurry
jonurry / 7-3 Eloquent Javascript Solutions.js
Created March 15, 2018 11:41
7.3 Persistent Group (Eloquent JavaScript Solutions)
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);
@jonurry
jonurry / 7-2 Eloquent Javascript Solutions.js
Created March 7, 2018 11:59
7.2 Robot Efficiency (Eloquent JavaScript Solutions)
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;
}
}