|
| 1 | +# Instructions |
| 2 | + |
| 3 | +Elena is the new quality manager of a newspaper factory. As she has just arrived in the company, she has decided to review some of the processes in the factory to see what could be improved. She found out that technicians are doing a lot of quality checks by hand. She sees there is a good oportunity for automation and asks you, a freelance developer, to develop a piece of software to monitor some of the machines. |
| 4 | + |
| 5 | +## Check the humidity level of the production room |
| 6 | + |
| 7 | +Your first mission is to write a piece of software to monitor the humidity level of the production room. There is already a sensor connected to the software of the company that returns periodically the humidity percentage of the room. You need to implement a function in the software that will throw an error if the humidity percentage is too high. |
| 8 | + |
| 9 | +## Detect overheating and broken sensors |
| 10 | + |
| 11 | +Elena is very pleased with your first assignement and ask you to deal with the monitoring of the machines' temperature. |
| 12 | + |
| 13 | +While chatting with a technician, Greg, you are told that if the temperature of a machine exceeds 500°C, the technicians start worrying about overheating. |
| 14 | + |
| 15 | +The machine is equiped with a sensor that measures its internal temperature. You should know that the sensor is very sensitive and often breaks. In this case the technicians will need to change it. |
| 16 | + |
| 17 | +Your job is to implement a function that throws an error if the sensor is broken or if the machine starts overheating. |
| 18 | + |
| 19 | +Knowing that you will later need to react differently depending on the error, you need a mechanism to differentiate the two kind of errors. |
| 20 | + |
| 21 | +You could rely on the error messages, but this would make your code fragile as it would break if the message gets updated. |
| 22 | + |
| 23 | +So to be able to do so properly, you'll throw instances of different error classes. |
| 24 | + |
| 25 | +## Catching errors |
| 26 | + |
| 27 | +Now that your machine is able to detect errors, you will implement a function that reacts to those errors in different ways : |
| 28 | + |
| 29 | +- If the sensor is broken, you need to warn a technician |
| 30 | +- If the temperature is too high, you will either shutdown the machine if the temperature exceeds 600°C or turn on a warning light if it is less than that. |
| 31 | +- If another error happens, you'll rethrow it. |
| 32 | + |
| 33 | +## Tasks |
| 34 | + |
| 35 | +## 1. Monitor the humidity level of the room |
| 36 | + |
| 37 | +Implements a function `checkHumidityLevel` that takes the humidity percentage as a parameter. |
| 38 | + |
| 39 | +You should throw an error (the message isn't important) if the percentage exceeds 70%. |
| 40 | + |
| 41 | +```javascript |
| 42 | +checkHumidityLevel(60); |
| 43 | +// Returns undefined |
| 44 | +``` |
| 45 | + |
| 46 | +```javascript |
| 47 | +checkHumidityLevel(100); |
| 48 | +// Throws an error |
| 49 | +``` |
| 50 | + |
| 51 | +## 2. Detect overheating |
| 52 | + |
| 53 | +Implements a function `reportOverheating` that takes the temperature as a parameter. |
| 54 | + |
| 55 | +If the sensor is broken, the temperature will be null. In this case you should throw an `ArgumentError`. |
| 56 | + |
| 57 | +When everything works, if the temperature exceeds 500°C, you should throw a `OverheatingError`. This error class will be instanciated with a temperature argument. Make sure that the `OverheatingError` you throw has a temperature property attached to it. |
| 58 | + |
| 59 | +```javascript |
| 60 | +reportOverheating(null); |
| 61 | +// Throws an ArgumentError |
| 62 | +``` |
| 63 | + |
| 64 | +```javascript |
| 65 | +reportOverheating(800); |
| 66 | +// Throws an OverheatingError |
| 67 | +``` |
| 68 | + |
| 69 | +## 3. Monitor the machine |
| 70 | + |
| 71 | +Implements a function `monitorTheMachine` that takes an argument `actions`. |
| 72 | + |
| 73 | +`actions` is an object that has 4 properties : |
| 74 | + |
| 75 | +- `check` is a _*function*_ that, when called, checks the temperature of the machine. |
| 76 | + It may throw various errors |
| 77 | + |
| 78 | +- `alertDeadSensor` is a _*function*_ that, when called, alerts a technician that the temperature's sensor is dead. |
| 79 | + |
| 80 | +- `alertOverheating` is a _*function*_ that, when called, will turn on a warning light on the machine. |
| 81 | + |
| 82 | +- `shutdown` is a _*function*_ that, when called, will turn off the machine. |
| 83 | + |
| 84 | +The `monitorTheMachine` function should call `check()`. If it passes, the function should not return anything. However it may `throw` an error. When this happens, you should, depending on the error: |
| 85 | + |
| 86 | +- `ArgumentError`: when this happens, call the `alertDeadSensor` function. |
| 87 | +- `OverheatingError`: when this happens, if the temperature is less than 600 °C, call the `alertOverheating` function to turn on the warning light. If the temperature exceeds 600°C, the situation is critical, call the `shutdown` function. |
| 88 | +- _anything else_: when this happens, rethrow the error |
| 89 | + |
| 90 | +```javascript |
| 91 | +monitorTheMachine({ |
| 92 | + check, |
| 93 | + alertDeadSensor, |
| 94 | + alertOverheating, |
| 95 | + shutdown, |
| 96 | +}); |
| 97 | +``` |
0 commit comments