{ "name": "Debugging", "order": 4, "time": "1 hour", "helpRoom": "Help", "challenges": [ { "id": "587d7b83367417b2b2512b32", "title": "Introduction to the Debugging Challenges", "description": [ [ "http://imgs.xkcd.com/comics/debugging.png", "XKCD web comic walking through the convoluted process of debugging. The programmer moves from a browser problem, to a keyboard driver issue, receives a cryptic system utility error message, and ends by finding the sword of Martin the Warrior.", "Debugging is a valuable and (unfortunately) necessary tool for programmers. It follows the testing phase of checking if your code works as intended, and discovering it does not. Debugging is the process of finding exactly what isn't working and fixing it.", "" ], [ "", "", "After spending time creating a brilliant block of code, it is tough realizing it may have errors. These issues generally come in three forms: 1) syntax errors that prevent a program from running, 2) runtime errors when code fails to execute or has unexpected behavior, and 3) semantic (or logical) errors when code doesn't do what it's meant to.

Modern code editors (and experience) can help identify syntax errors. Semantic and runtime errors are harder to find. They may cause your program to crash, make it run forever, or give incorrect output. Think of debugging as trying to understand why your code is behaving the way it is.

Example of a syntax error - often detected by the code editor:

funtion willNotWork( {
  console.log(\"Yuck\");
}
// \"function\" keyword is misspelled and there's a missing parenthesis


Here's an example of a runtime error - often detected while the program executes:

function loopy() {
  while(true) {
    console.log(\"Hello, world!\");
  }
}
// Calling loopy starts an infinite loop, which may crash your browser


Example of a semantic error - often detected after testing code output:

function calcAreaOfRect(w, h) {
  return w + h; // This should be w * h
}
let myRectArea = calcAreaOfRect(2, 3);
// Correct syntax and the program executes, but this gives the wrong answer
", "" ], [ "", "", "Debugging is frustrating, but it helps to develop (and follow) a step-by-step approach to review your code. This means checking the intermediate values and types of variables to see if they are what they should be. You can start with a simple process of elimination.

For example, if function A works and returns what it's supposed to, then function B may have the issue. Or start checking values in a block of code from the middle to try to cut the search space in half. A problem in one spot indicates a bug in the first half of the code. If not, it's likely in the second.

This section will cover a couple helpful tools to find bugs, and some of the common forms they take. Fortunately, debugging is a learnable skill that just requires a little patience and practice to master.", "" ] ], "releasedOn": "Feb 17, 2017", "challengeSeed": [], "tests": [], "type": "waypoint", "challengeType": 7, "isRequired": false, "translations": {} }, { "id": "587d7b83367417b2b2512b33", "title": "Use the JavaScript Console to Check the Value of a Variable", "description": [ "Both Chrome and Firefox have excellent JavaScript consoles, also known as DevTools, for debugging your JavaScript.", "You can find Developer tools in your Chrome's menu or Web Console in FireFox's menu. If you're using a different browser, or a mobile phone, we strongly recommend switching to desktop Firefox or Chrome.", "The console.log() method, which \"prints\" the output of what's within its parentheses to the console, will likely be the most helpful debugging tool. Placing it at strategic points in your code can show you the intermediate values of variables. It's good practice to have an idea of what the output should be before looking at what it is. Having check points to see the status of your calculations throughout your code will help narrow down where the problem is.", "Here's an example to print 'Hello world!' to the console:", "console.log('Hello world!');", "
", "Use the console.log() method to print the value of the variable a where noted in the code." ], "challengeSeed": [ "let a = 5;", "let b = 1;", "a++;", "// Add your code below this line", "", "", "let sumAB = a + b;", "console.log(sumAB);" ], "tests": [ "assert(code.match(/console\\.log\\(a\\)/g), 'message: Your code should use console.log() to check the value of the variable a.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b84367417b2b2512b34", "title": "Use typeof to Check the Type of a Variable", "description": [ "You can use typeof to check the data structure, or type, of a variable. This is useful in debugging when working with multiple data types. If you think you're adding two numbers, but one is actually a string, the results can be unexpected. Type errors can lurk in calculations or function calls. Especially take care when you're accessing and working with external data in the form of a JavaScript object (JSON).", "Here are some examples using typeof:", "
console.log(typeof \"\"); // outputs \"string\"
console.log(typeof 0); // outputs \"number\"
console.log(typeof []); // outputs \"object\"
console.log(typeof {}); // outputs \"object\"
", "JavaScript recognizes six primitive (immutable) data types: Boolean, Null, Undefined, Number, String, and Symbol (new with ES6) and one type for mutable items: Object. Note that in JavaScript, arrays are technically a type of object.", "
", "Add two console.log() statements to check the typeof each of the two variables seven and three in the code." ], "challengeSeed": [ "let seven = 7;", "let three = \"3\";", "console.log(seven + three);", "// Add your code below this line", "" ], "tests": [ "assert(code.match(/console\\.log\\(typeof[\\( ].*\\)?\\);/g).length == 2, 'message: Your code should use typeof in two console.log() statements to check the type of the variables.');", "assert(code.match(/typeof[\\( ]seven\\)?/g), 'message: Your code should use typeof to check the type of the variable seven.');", "assert(code.match(/typeof[\\( ]three\\)?/g), 'message: Your code should use typeof to check the type of the variable three.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b84367417b2b2512b35", "title": "Catch Misspelled Variable and Function Names", "description": [ "The console.log() and typeof methods are the two primary ways to check intermediate values and types of program output. Now it's time to get into the common forms that bugs take. One syntax-level issue that fast typers can commiserate with is the humble spelling error.", "Transposed, missing, or mis-capitalized characters in a variable or function name will have the browser looking for an object that doesn't exist - and complain in the form of a reference error. JavaScript variable and function names are case-sensitive.", "
", "Fix the two spelling errors in the code so the netWorkingCapital calculation works." ], "challengeSeed": [ "let receivables = 10;", "let payables = 8;", "let netWorkingCapital = recievables - payable;", "console.log(`Net working capital is: ${netWorkingCapital}`);" ], "tests": [ "assert(netWorkingCapital === 2, 'message: Check the spelling of the two variables used in the netWorkingCapital calculation, the console output should show that \"Net working capital is: 2\".');", "assert(!code.match(/recievables/g), 'message: There should be no instances of mis-spelled variables in the code.');", "assert(code.match(/receivables/g).length == 2, 'message: The receivables variable should be declared and used properly in the code.');", "assert(!code.match(/payable;/g), 'message: There should be no instances of mis-spelled variables in the code.');", "assert(code.match(/payables/g).length == 2, 'message: The payables variable should be declared and used properly in the code.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b84367417b2b2512b36", "title": "Catch Unclosed Parentheses, Brackets, Braces and Quotes", "description": [ "Another syntax error to be aware of is that all opening parentheses, brackets, curly braces, and quotes have a closing pair. Forgetting a piece tends to happen when you're editing existing code and inserting items with one of the pair types. Also, take care when nesting code blocks into others, such as adding a callback function as an argument to a method.", "One way to avoid this mistake is as soon as the opening character is typed, immediately include the closing match, then move the cursor back between them and continue coding. Fortunately, most modern code editors generate the second half of the pair automatically.", "
", "Fix the two pair errors in the code." ], "challengeSeed": [ "let myArray = [1, 2, 3;", "let arraySum = myArray.reduce((previous, current => previous + current);", "console.log(`Sum of array values is: ${arraySum}`);" ], "tests": [ "assert(code.match(/myArray\\s*?=\\s*?\\[\\s*?1\\s*?,\\s*?2\\s*?,\\s*?3\\s*?\\];/g), 'message: Your code should fix the missing piece of the array.');", "assert(arraySum === 6, 'message: Your code should fix the missing piece of the .reduce() method. The console output should show that \"Sum of array values is: 6\".');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b84367417b2b2512b37", "title": "Catch Mixed Usage of Single and Double Quotes", "description": [ "JavaScript allows the use of both single ('') and double (\"\") quotes to declare a string. Deciding which one to use generally comes down to personal preference, with some exceptions.", "Having two choices is great when a string has contractions or another piece of text that's in quotes. Just be careful that you don't close the string too early, which causes a syntax error.", "Here are some examples of mixing quotes:", "
// These are correct:
const grouchoContraction = \"I've had a perfectly wonderful evening, but this wasn't it.\";
const quoteInString = \"Groucho Marx once said 'Quote me as saying I was mis-quoted.'\";
// This is incorrect:
const uhOhGroucho = 'I've had a perfectly wonderful evening, but this wasn't it.';
", "Of course, it is okay to use only one style of quotes. You can escape the quotes inside the string by using the backslash (\\) escape character:", "
// Correct use of same quotes:
const allSameQuotes = 'I\\'ve had a perfectly wonderful evening, but this wasn\\'t it.';
", "
", "Fix the string so it either uses different quotes for the href value, or escape the existing ones. Keep the double quote marks around the entire string." ], "challengeSeed": [ "let innerHtml = \"

Click here to return home

\";", "console.log(innerHtml);" ], "tests": [ "assert(code.match(//g), 'message: Your code should fix the quotes around the href value \"#Home\" by either changing or escaping them.');", "assert(code.match(/\"

.*?<\\/p>\";/g), 'message: Your code should keep the double quotes around the entire string.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b85367417b2b2512b38", "title": "Catch Use of Assignment Operator Instead of Equality Operator", "description": [ "Branching programs, i.e. ones that do different things if certain conditions are met, rely on if, else if, and else statements in JavaScript. The condition sometimes takes the form of testing whether a result is equal to a value.", "This logic is spoken (in English, at least) as \"if x equals y, then ...\" which can literally translate into code using the =, or assignment operator. This leads to unexpected control flow in your program.", "As covered in previous challenges, the assignment operator (=) in JavaScript assigns a value to a variable name. And the == and === operators check for equality (the triple === tests for strict equality, meaning both value and type are the same).", "The code below assigns x to be 2, which evaluates as true. Almost every value on its own in JavaScript evaluates to true, except what are known as the \"falsy\" values: false, 0, \"\" (an empty string), NaN, undefined, and null.", "

let x = 1;
let y = 2;
if (x = y) {
  // this code block will run for any value of y (unless y were originally set as a falsy)
} else {
  // this code block is what should run (but won't) in this example
}
", "
", "Fix the condition so the program runs the right branch, and the appropriate value is assigned to result." ], "challengeSeed": [ "let x = 7;", "let y = 9;", "let result = \"to come\";", "", "if(x = y) {", " result = \"Equal!\";", "} else {", " result = \"Not equal!\";", "}", "", "console.log(result);" ], "tests": [ "assert(result == \"Not equal!\", 'message: Your code should fix the condition so it checks for equality, instead of using assignment.');", "assert(code.match(/x\\s*?===?\\s*?y/g), 'message: The condition can use either == or === to test for equality.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b85367417b2b2512b39", "title": "Catch Missing Open and Closing Parenthesis After a Function Call", "description": [ "When a function or method doesn't take any arguments, you may forget to include the (empty) opening and closing parentheses when calling it. Often times the result of a function call is saved in a variable for other use in your code. This error can be detected by logging variable values (or their types) to the console and seeing that one is set to a function reference, instead of the expected value the function returns.", "The variables in the following example are different:", "
function myFunction() {
  return \"You rock!\";
}
let varOne = myFunction; // set to equal a function
let varTwo = myFunction(); // set to equal the string \"You rock!\"
", "
", "Fix the code so the variable result is set to the value returned from calling the function getNine." ], "challengeSeed": [ "function getNine() {", " let x = 6;", " let y = 3;", " return x + y;", "}", "", "let result = getNine;", "console.log(result);" ], "tests": [ "assert(result == 9, 'message: Your code should fix the variable result so it is set to the number that the function getNine returns.');", "assert(code.match(/getNine\\(\\)/g).length == 2, 'message: Your code should call the getNine function.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b85367417b2b2512b3a", "title": "Catch Arguments Passed in the Wrong Order When Calling a Function", "description": [ "Continuing the discussion on calling functions, the next bug to watch out for is when a function's arguments are supplied in the incorrect order. If the arguments are different types, such as a function expecting an array and an integer, this will likely throw a runtime error. If the arguments are the same type (all integers, for example), then the logic of the code won't make sense. Make sure to supply all required arguments, in the proper order to avoid these issues.", "
", "The function raiseToPower raises a base to an exponent. Unfortunately, it's not called properly - fix the code so the value of power is the expected 8." ], "challengeSeed": [ "function raiseToPower(b, e) {", " return Math.pow(b, e);", "}", "", "let base = 2;", "let exp = 3;", "let power = raiseToPower(exp, base);", "console.log(power);" ], "tests": [ "assert(power == 8, 'message: Your code should fix the variable power so it equals 2 raised to the 3rd power, not 3 raised to the 2nd power.');", "assert(code.match(/raiseToPower\\(\\s*?base\\s*?,\\s*?exp\\s*?\\);/g), 'message: Your code should use the correct order of the arguments for the raiseToPower function call.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b86367417b2b2512b3b", "title": "Catch Off By One Errors When Using Indexing", "description": [ "Off by one errors (sometimes called OBOE) crop up when you're trying to target a specific index of a string or array (to slice or access a segment), or when looping over the indices of them. JavaScript indexing starts at zero, not one, which means the last index is always one less than the length of the item. If you try to access an index equal to the length, the program may throw an \"index out of range\" reference error or print undefined.", "When you use string or array methods that take index ranges as arguments, it helps to read the documentation and understand if they are inclusive (the item at the given index is part of what's returned) or not. Here are some examples of off by one errors:", "
let alphabet = \"abcdefghijklmnopqrstuvwxyz\";
let len = alphabet.length;
for (let i = 0; i <= len; i++) {
  // loops one too many times at the end
  console.log(alphabet[i]);
}
for (let j = 1; j < len; j++) {
  // loops one too few times and misses the first character at index 0
  console.log(alphabet[j]);
}
for (let k = 0; k < len; k++) {
  // Goldilocks approves - this is just right
  console.log(alphabet[k]);
}
", "
", "Fix the two indexing errors in the following function so all the numbers 1 through 5 are printed to the console." ], "challengeSeed": [ "function countToFive() {", " let firstFive = \"12345\";", " let len = firstFive.length;", " // Fix the line below", " for (let i = 1; i <= len; i++) {", " // Do not alter code below this line", " console.log(firstFive[i]);", " }", "}", "", "countToFive();" ], "tests": [ "assert(code.match(/i\\s*?=\\s*?0\\s*?;/g).length == 1, 'message: Your code should set the initial condition of the loop so it starts at the first index.');", "assert(!code.match(/i\\s?=\\s*?1\\s*?;/g), 'message: Your code should fix the initial condition of the loop so that the index starts at 0.');", "assert(code.match(/i\\s*?<\\s*?len\\s*?;/g).length == 1, 'message: Your code should set the terminal condition of the loop so it stops at the last index.');", "assert(!code.match(/i\\s*?<=\\s*?len;/g), 'message: Your code should fix the terminal condition of the loop so that it stops at 1 before the length.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b86367417b2b2512b3c", "title": "Use Caution When Reinitializing Variables Inside a Loop", "description": [ "Sometimes it's necessary to save information, increment counters, or re-set variables within a loop. A potential issue is when variables either should be reinitialized, and aren't, or vice versa. This is particularly dangerous if you accidentally reset the variable being used for the terminal condition, causing an infinite loop.", "Printing variable values with each cycle of your loop by using console.log() can uncover buggy behavior related to resetting, or failing to reset a variable.", "
", "The following function is supposed to create a two-dimensional array with m rows and n columns of zeroes. Unfortunately, it's not producing the expected output because the row variable isn't being reinitialized (set back to an empty array) in the outer loop. Fix the code so it returns a correct 3x2 array of zeroes, which looks like [[0, 0], [0, 0], [0, 0]]." ], "challengeSeed": [ "function zeroArray(m, n) {", " // Creates a 2-D array with m rows and n columns of zeroes", " let newArray = [];", " let row = [];", " for (let i = 0; i < m; i++) {", " // Adds the m-th row into newArray", " ", " for (let j = 0; j < n; j++) {", " // Pushes n zeroes into the current row to create the columns", " row.push(0);", " }", " // Pushes the current row, which now has n zeroes in it, to the array", " newArray.push(row);", " }", " return newArray;", "}", "", "let matrix = zeroArray(3, 2);", "console.log(matrix);" ], "tests": [ "assert(JSON.stringify(matrix) == \"[[0,0],[0,0],[0,0]]\", 'message: Your code should set the matrix variable to an array holding 3 rows of 2 columns of zeroes each.');", "assert(matrix.length == 3, 'message: The matrix variable should have 3 rows.');", "assert(matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2, 'message: The matrix variable should have 2 columns in each row.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} }, { "id": "587d7b86367417b2b2512b3d", "title": "Prevent Infinite Loops with a Valid Terminal Condition", "description": [ "The final topic is the dreaded infinite loop. Loops are great tools when you need your program to run a code block a certain number of times or until a condition is met, but they need a terminal condition that ends the looping. Infinite loops are likely to freeze or crash the browser, and cause general program execution mayhem, which no one wants.", "There was an example of an infinite loop in the introduction to this section - it had no terminal condition to break out of the while loop inside loopy(). Do NOT call this function!", "
function loopy() {
  while(true) {
    console.log(\"Hello, world!\");
  }
}
", "It's the programmer's job to ensure that the terminal condition, which tells the program when to break out of the loop code, is eventually reached. One error is incrementing or decrementing a counter variable in the wrong direction from the terminal condition. Another one is accidentally resetting a counter or index variable within the loop code, instead of incrementing or decrementing it.", "
", "The myFunc() function contains an infinite loop because the terminal condition i != 4 will never evaluate to false (and break the looping) - i will increment by 2 each pass, and jump right over 4 since i is odd to start. Fix the comparison operator in the terminal condition so the loop only runs for i less than or equal to 4." ], "challengeSeed": [ "function myFunc() {", " for (let i = 1; i != 4; i += 2) {", " console.log(\"Still going!\");", " }", "}" ], "tests": [ "assert(code.match(/i\\s*?<=\\s*?4;/g).length == 1, 'message: Your code should change the comparison operator in the terminal condition (the middle part) of the for loop.');", "assert(!code.match(/i\\s*?!=\\s*?4;/g), 'message: Your code should fix the comparison operator in the terminal condition of the loop.');" ], "solutions": [], "hints": [], "type": "waypoint", "releasedOn": "Feb 17, 2017", "challengeType": 1, "translations": {} } ] }