Exercise Solutions: Booleans and ConditionalsΒΆ
Declare and initialize the following variables for our space shuttle.
1 2 3 4 5 6
let engineIndicatorLight = "red blinking"; let spaceSuitsOn = true; let shuttleCabinReady = true; let crewStatus = spaceSuitsOn && shuttleCabinReady; let computerStatusCode = 200; let shuttleSpeed = 15000;
Write conditional expressions to satisfy the safety rules.
1 2 3 4 5
if (crewStatus) { console.log("Crew Ready"); } else { console.log("Crew Not Ready"); }
1 2 3 4 5 6 7
if (computerStatusCode === 200) { console.log("Please stand by. Computer is rebooting."); } else if (computerStatusCode === 400) { console.log("Success! Computer online."); } else { console.log("ALERT: Computer offline!"); }
1 2 3 4 5 6 7
if (shuttleSpeed > 17500) { console.log("ALERT: Escape velocity reached!"); } else if (shuttleSpeed < 8000) { console.log("ALERT: Cannot maintain orbit!"); } else { console.log("Stable speed."); }
Monitor the shuttle's fuel status.
1 2 3 4 5 6 7 8 9 10 11 12 13
if (fuelLevel < 1000 || engineTemperature > 3500 || engineIndicatorLight === "red blinking") { console.log("ENGINE FAILURE IMMINENT!"); } else if (fuelLevel <= 5000 || engineTemperature > 2500) { console.log("Check fuel level. Engines running hot."); } else if (fuelLevel > 20000 && engineTemperature <= 2500) { console.log("Full tank. Engines good."); } else if (fuelLevel > 10000 && engineTemperature <= 2500) { console.log("Fuel level above 50%. Engines good."); } else if (fuelLevel > 5000 && engineTemperature <= 2500) { console.log("Fuel level above 25%. Engines good."); } else { console.log("Fuel and engine status pending..."); }