Skip to content

Instantly share code, notes, and snippets.

@spamwax
Last active August 29, 2015 14:02
Show Gist options
  • Save spamwax/53df78f3f3277b2fb65a to your computer and use it in GitHub Desktop.
Save spamwax/53df78f3f3277b2fb65a to your computer and use it in GitHub Desktop.
Solution to eloquentjavascript exercise second 2nd edition (Chapter 8) eloquent javascript
// Solution to eloquentjavascript exercise second 2nd edition
// Chapter 6
// http://eloquentjavascript.net/2nd_edition/preview/08_error.html
// Retry
function MultiplicatorUnitFailure() {}
function primitiveMultiply(a, b) {
if (Math.random() < 0.5)
return a * b;
else
throw new MultiplicatorUnitFailure();
}
function reliableMultiply1(a, b) {
try {
return primitiveMultiply(a, b);
} catch (e) {
if (e instanceof MultiplicatorUnitFailure) {
return reliableMultiply(a, b);
} else {
throw e;
}
}
}
function reliableMultiply2(a, b) {
for (;;) {
try {
return primitiveMultiply(a, b);
} catch (e) {
if (e instanceof MultiplicatorUnitFailure) {
continue;
} else
throw e;
}
}
}
console.log(reliableMultiply1(8, 8));
console.log(reliableMultiply1(6, 6));
// ---------------------------------------
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment