Skip to content

Commit

Permalink
Chapter 8 Exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
EMCestari committed Apr 4, 2022
1 parent 67b1bd3 commit 25de2c5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Chapter 8 - Bugs and Errors/Retry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Retry
- You have a function primitiveMultiply: in 20 percent of cases multiplies two numbers and in the other 80 percent
of cases raises an exception of type MultiplicatorUnitFailure
- Write a function that wraps this clunky function and just keeps trying until a call succeeds,
after which it returns the result.
Make sure you handle only the exceptions you are trying to handle.
*/

const primitiveMultiply = (numberOne, numberTwo) => {
let randomPercentage = Math.floor(Math.random() * 100) + 1;
if (randomPercentage <= 20) { return numberOne * numberTwo }
else throw new Error("MultiplicatorUnitFailure");
};

const retry = () => {
let result = undefined;
while (result === undefined){
try{
result = primitiveMultiply(2,4);
} catch(error){
if(error === "MultiplicatorUnitFailure");
continue;
}
}
return result;
}
67 changes: 67 additions & 0 deletions Chapter 8 - Bugs and Errors/TheLockedBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
The Locked Box
Consider the object "box":
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;
}
};
It is a box with a lock.
There is an array in the box, but you can get at it only when the box is unlocked.
Directly accessing the private _content property is forbidden.
- Write a function called withBoxUnlocked that takes a function value as argument,
unlocks the box, runs the function, and then ensures that the box is locked again before returning,
regardless of whether the argument function returned normally or threw an exception.
- For extra points, make sure that if you call withBoxUnlocked when the box is already unlocked,
the box stays unlocked.
*/

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;
}
};


function withBoxUnlocked(body){
console.log("At the beginning box is locked: " + box.locked);
let boxInitialStatus = box.locked;
if (box.locked){
box.unlock();
}
try {
body();
} catch (e) {
console.log("Error Raised: ", e)
} finally {
if (boxInitialStatus === true) box.lock();
}
console.log("At the end box is locked: " + box.locked);
}

withBoxUnlocked(function (){
box.content.push("gold piece");
});

try {
withBoxUnlocked(function() {
throw new Error("Pirates on the horizon! Abort!");
});

} catch (e) {
console.log("Error raised:", e);
}

0 comments on commit 25de2c5

Please sign in to comment.