Last active
August 29, 2015 14:02
-
-
Save spamwax/53df78f3f3277b2fb65a to your computer and use it in GitHub Desktop.
Solution to eloquentjavascript exercise second 2nd edition (Chapter 8) eloquent javascript
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 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