-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
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
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; | ||
} |
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
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); | ||
} |