Exercise Solutions: Errors and DebuggingΒΆ
Line 4 needs a closing parenthesis:
4
if (fuelLevel >= 20000) {
fuellevelshould befuelLevelon Line 7:7
if (fuelLevel >= 20000) {
Check your logic
Should the shuttle have launched? Did it?
The shuttle should not have launched. However, the messages to the console tell a different story. Without any changes, the original code outputs:
WARNING: Insufficient fuel! Crew & computer cleared. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1... Liftoff!
Given
crewStatusandcomputerStatus, shouldlaunchReadybe true or false after this check?With their initial values set to
trueand'green', line 14 evaluates totrueandlaunchReadyis set totrue. If it's value on dependent on the value of these variables only (crewStatusandcomputerStatus), thenlaunchReadyshould betrueafter this check.
Repair the launch readiness checks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
let launchReady = false; let crewReady = false; let fuelLevel = 17000; let crewStatus = true; let computerStatus = 'green'; if (fuelLevel >= 20000) { console.log('Fuel level cleared.'); launchReady = true; } else { console.log('WARNING: Insufficient fuel!'); launchReady = false; } console.log("launchReady = ", launchReady); if (crewStatus && computerStatus === 'green'){ console.log('Crew & computer cleared.'); crewReady = true; } else { console.log('WARNING: Crew or computer not ready!'); crewReady = false; } console.log("crewReady = ", crewReady);
