Walkthrough and solutions. This article will walk you through freecodecamp.org debugging challenges.
-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. — by FreeCodeCamp
You can find the debugging challenges here
You can also watch the video where I talk and code the first 4 debugging challenges.
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.
The solution → console.log(a);
To clear the console use console.clear().
The solution → console.log(outputTwo); and console.clear() and console.log(outputOne);
The solution → console.log(typeof seven); and console.log(typeof three);
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.
The solution → correct spelling at: receivables and payables
You can also watch the video where I talk and code the next 4 debug challenges.
A 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.
The solution →
line 1: ]
line 2: )
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.
The solution → single quotes, ‘#Home’
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.
The solution → line 5: if(x==y)
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 solution → line 7: getNine()
You can also watch the last part of the video series where I talk and code the debug challenges.
You may also have 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 solution → line 7: raiseToPower(base, exp)
The solution → line 5: for (let i=0; i<len; i++)
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 solution →
line 14: row=
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.
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 solution → line 2: (let i=1; i≤4; i+=2)
Are you still here?! Well, if you are, congratulations! You just finished the Debugging challenges by freecodecamp!
Disclaimer: Debugging definitions are by freecodecamp.org