Exercises: Debugging
Imagine we are running a space station. Your job is to evaluate the station’s code and fix any errors. The lives of the crew rest squarely upon your shoulders.
Your directions from superiors:
- Launch the shuttle only if the fuel, crew and computer all check out OK.
- If a check fails, print that information to the console and scrub the launch.
- If all checks are successful, print a countdown to launch in the console.
To get started, check out the exercises directory in javascript-projects/errors-and-debugging.
Debugging Practice
Fix syntax errors first. Run the code in
Debugging1stSyntaxError.jsas-is and read the error message. Fix the mistake, and then re-run the code to check it.1 2 3 4 5 6 7 8 9 10let launchReady = false; let fuelLevel = 17000; if (fuelLevel >= 20000 { console.log('Fuel level cleared.'); launchReady = true; } else { console.log('WARNING: Insufficient fuel!'); launchReady = false; }The next block of code in
DebuggingSyntaxErrors2.jshides two syntax errors. Run the code as-is to find the mistakes.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20let launchReady = false; let crewStatus = true; let computerStatus = 'green'; if (crewStatus &&& computerStatus === 'green') { console.log('Crew & computer cleared.'); launchReady = true; } else { console.log('WARNING: Crew or computer not ready!'); launchReady = false; } if (launchReady) { console.log(("10, 9, 8, 7, 6, 5, 4, 3, 2, 1..."); console.log("Fed parrot..."); console.log("Ignition..."); console.log("Liftoff!"); } else { console.log("Launch scrubbed."); }TipOnly one error will be flagged at a time. Fix that ONE problem, and then re-run the code to check your work. Avoid trying to fix multiple issues at once.
Fix runtime errors next. Open up
DebuggingRuntimeErrors1.js. Remember to examine the error message for clues about what is going wrong. Pay close attention to any line numbers mentioned in the message - these will help you locate and repair the mistake in the code.1 2 3 4 5 6 7 8 9 10let launchReady = false; let fuelLevel = 17000; if (fuellevel >= 20000) { console.log('Fuel level cleared.'); launchReady = true; } else { console.log('WARNING: Insufficient fuel!'); launchReady = false; }Arrr! Did we mention your crew are space pirates? Open up
DebuggingRuntimeErrors2.js. Now find and fix the runtime error in a longer code sample.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21let launchReady = false; let fuelLevel = 27000; if (fuelLevel >= 20000) { console.log('Fuel level cleared.'); launchReady = true; } else { console.log('WARNING: Insufficient fuel!'); launchReady = false; } if (launchReady) { console.log("10, 9, 8..."); console.log("Fed parrot..."); console.log("6, 5, 4..."); console.log("Ignition..."); consoul.log("3, 2, 1..."); console.log("Liftoff!"); } else { console.log("Launch scrubbed."); }Solve logic errors last. Logic errors do not generate warning messages or prevent the code from running, but the program still does not work as intended. (Refer to debugging logic errors if you need to review).
Open up
DebuggingLogicErrors1.js. First, run this sample code as-is and examine the output.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27let launchReady = 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; } if (crewStatus && computerStatus === 'green'){ console.log('Crew & computer cleared.'); launchReady = true; } else { console.log('WARNING: Crew or computer not ready!'); launchReady = false; } if (launchReady) { console.log('10, 9, 8, 7, 6, 5, 4, 3, 2, 1...'); console.log('Liftoff!'); } else { console.log('Launch scrubbed.'); }Should the shuttle have launched? Did it?
Let’s break the code down into smaller chunks. Open up
DebuggingLogicErrors2.js. Consider the firstif/elseblock below. We’ve commented out some of the variables we’re not inspecting right now. Addconsole.log(launchReady)after this block, then run the program.1 2 3 4 5 6 7 8 9 10 11 12let launchReady = 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; }Given the
fuelLevelvalue, shouldlaunchReadybetrueorfalseafter the check? Is the program behaving as expected?Now consider the second
if/elseblock. OPen upDebuggingLogicErrors3.js. Here again, we comment the variables and blocks that we’re not inspecting. Add anotherconsole.log(launchReady)after this block and run the program.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20let launchReady = 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; // } if (crewStatus && computerStatus === 'green'){ console.log('Crew & computer cleared.'); launchReady = true; } else { console.log('WARNING: Crew or computer not ready!'); launchReady = false; }Given
crewStatusandcomputerStatus, shouldlaunchReadybetrueorfalseafter this check?Is the program behaving as expected?
Now consider both
if/elseblocks together (keeping the addedconsole.loglines). Run the code inDebuggingLogicErrors4.jsand examine the output.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22let launchReady = 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); if (crewStatus && computerStatus === 'green'){ console.log('Crew & computer cleared.'); launchReady = true; } else { console.log('WARNING: Crew or computer not ready!'); launchReady = false; } console.log(launchReady);Given the values for
fuelLevel,crewStatusandcomputerStatus, shouldlaunchReadybetrueorfalse? Is the program behaving as expected?Ahoy, Houston! We spied a problem! The value of
launchReadyassigned in the firstif/elseblock got changed in the secondif/elseblock. Dangerous waters, Matey. Open upDebuggingLogicErrors5.jsto address this error.The issue is with the
launchReadyvalue being assigned and reassigned based on different checks. One way to fix the logic error is to use two different variables to store the results of checking the fuel readiness (lines 6-13) and checking the crew and computer readiness (lines 15-22).Update your code to do this. Verify that your change works by updating the
console.logstatements.
Almost done, so wipe the sweat off your brow! Add a final
if/elseblock to print a countdown and “Liftoff!” if all the checks pass, or print “Launch scrubbed” if any check fails.Blimey! That’s some good work.