5.6. Exercises: Booleans and ConditionalsΒΆ
Attempt these exercises to test your understanding. Don't worry if you struggle while working on them. Struggling and then reviewing the material will help you remember it.
Take note of any problem here or topic from this chapter that you don't understand. Take a break and return to the problem. Do you see it in a new way or have a better understanding? If not, try spending five minutes researching the topic. Start with this book and if you still have questions, ask one on the internet or Slack/Discourse. You're not the first person to learn to code and you're definitely not the first person to ask a question or get stuck!
Declare and initialize the following variables for our space shuttle
Variable Name
Value
engineIndicatorLight
red blinking
spaceSuitsOn
true
shuttleCabinReady
true
crewStatus
spaceSuitsOn && shuttleCabinReady
computerStatusCode
200
shuttleSpeed
15000
Examine the code below. What will be printed to the console?
Use the value of
engineIndicatorLightdefined above to answer this question.1 2 3 4 5 6 7
if (engineIndicatorLight === "green") { console.log("engines have started"); } else if (engineIndicatorLight === "green blinking") { console.log("engines are preparing to start"); } else { console.log("engines are off"); }
Write conditional expressions to satisfy the safety rules.
Use the variables defined from the table above to satisfy the rules listed below.
crewStatusIf the value is
true, print"Crew Ready"Else print
"Crew Not Ready"
computerStatusCodeIf the value is
200, print"Please stand by. Computer is rebooting."Else if the value is
400, print"Success! Computer online."Else print
"ALERT: Computer offline!"
shuttleSpeedIf the value is
> 17500, print"ALERT: Escape velocity reached!"Else if the value is
< 8000, print"ALERT: Cannot maintain orbit!"Else print
"Stable speed"
PREDICT
Do these code blocks produce the same result? Answer Yes or No.
1 2 3 4 5
if (crewStatus && computerStatusCode === 200 && spaceSuitsOn) { console.log("all systems go"); } else { console.log("WARNING. Not ready"); }
1 2 3 4 5
if (!crewStatus || computerStatusCode !== 200 || !spaceSuitsOn) { console.log("WARNING. Not ready"); } else { console.log("all systems go"); }
Monitor the shuttle's fuel status.
Implement the checks below using
if/else if/elsestatements. Order is important when working with conditionals, and the checks below are NOT written in the correct sequence. Please read ALL of the checks before coding and then decide on the best order for the conditionals.If
fuelLevelis above 20000 ANDengineTemperatureis at or below 2500, print"Full tank. Engines good."If
fuelLevelis above 10000 ANDengineTemperatureis at or below 2500, print"Fuel level above 50%. Engines good."If
fuelLevelis above 5000 ANDengineTemperatureis at or below 2500, print"Fuel level above 25%. Engines good."If
fuelLevelis at or below 5000 ORengineTemperatureis above 2500, print"Check fuel level. Engines running hot."If
fuelLevelis below 1000 ORengineTemperatureis above 3500 ORengineIndicatorLightis red blinking, print"ENGINE FAILURE IMMINENT!"Otherwise, print
"Fuel and engine status pending..."
Try It
Run your code several times to make sure it prints the correct phrase for each set of conditions.
fuelLevel
engineTemperature
engineIndicatorLight
Result
Any
Any
red blinkingENGINE FAILURE IMMINENT!21000
1200
NOT
red blinkingFull tank. Engines good.900
Any
Any
ENGINE FAILURE IMMINENT!5000
1200
NOT
red blinkingCheck fuel level. Engines running hot.12000
2600
NOT
red blinkingCheck fuel level. Engines running hot.18000
2500
NOT
red blinkingFuel level above 50%. Engines good.
Final bit of fun!
The shuttle should only launch if the fuel tank is full and the engine check is OK. However, let's establish an override command to ignore any warnings and send the shuttle into space anyway!
Create the variable
commandOverride, and set it to betrueorfalse.If
commandOverrideisfalse, then the shuttle should only launch if the fuel and engine check are OK.If
commandOverrideistrue, then the shuttle will launch regardless of the fuel and engine status.Code the following
if/elsecheck:If
fuelLevelis above 20000 ANDengineIndicatorLightis NOT red blinking ORcommandOverrideis true print"Cleared to launch!"Else print
"Launch scrubbed!"
