Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://exercism.org/tracks/javascript/exercises/factory-sensors
- function checkHumidityLevel(percentage) {
- if (percentage > 70)
- throw new Error(`Humidity level is too high.`);
- }
- class ArgumentError extends Error {
- constructor(message) {
- super(message);
- }
- }
- class OverheatingError extends Error {
- temperature;
- constructor(message, temperature) {
- super(message);
- this.temperature = temperature;
- }
- }
- function reportOverheating(temperature) {
- if (temperature === null)
- throw new ArgumentError(`Sensor is broken.`);
- if (temperature > 500)
- throw new OverheatingError(`Temperature is too high.`, temperature);
- }
- function monitorTheMachine({ check, alertDeadSensor, alertOverheating, shutdown }) {
- try {
- check();
- return;
- } catch (error) {
- if (error instanceof ArgumentError)
- return alertDeadSensor();
- if (error instanceof OverheatingError)
- return (error.temperature < 600)
- ? alertOverheating()
- : shutdown();
- throw error;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement