- Construct Boolean expressions using the AND, OR and NOT operators.
Your Task, Your Challenge
Write your code so that the robot responds correctly based on the outside temperature.
The robot can check the outside temperature and you must ensure that it responds appropriately. If it is colder than 32°F, it shouldn't go out. If the temperature is 32°F or higher and less than 68°F, it should go out wearing a coat. Once it reaches 68°F, it should go out without a coat.
There are 2 tests to use:
- lessThan32() checks whether the outside temperature is less than 32°F;
- lessThan68() checks whether the outside temperature is less than 68°F.
Working with « Ranges »
The aim is to isolate the three possibilities with the most concise code possible and without using « else. » Since there are 3 possible actions (not going out, going out wearing a coat, or going out without a coat), 3 successive « if » statements should be sufficient.
When working with values, it is often helpful to view ranges graphically. For example, in the graphic below, the ranges in green correspond to the available tests. Below this, the areas in blue correspond to the robot's response based on the outside temperature.
Obviously, only one test is required to determine that the robot should not go out at all. The same is true for determining whether it should go out without a coat.
But the expression determining when to go out wearing a coat is a little more complex. This involves the intersection of times when the temperature is below 68°F but not lower than 32°F. These hints should help you to devise this Boolean expression.
Now all you have to do is write your code using the correct syntax and you'll be all set…
Refer to the chapter on theory just before this one if this is not yet clear to you.