Debug challenge

Walkthrough and solutions. This article will walk you through 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


Let’s start!

You can also watch the video where I talk and code the first 4 debugging challenges.

Debug, part 1/3


Use the JavaScript Console to Check the Value of a Variable

enlightened 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.

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.

angelThe solution → console.log(a);


Understanding the Differences between the freeCodeCamp and Browser Console

enlightenedWhen you load and run an ordinary JavaScript file in your browser the console.log() statements will print exactly what you tell them to print to the browser console the exact number of times you requested.

To clear the console use console.clear().

angelThe solution → console.log(outputTwo); and console.clear() and console.log(outputOne);


Use typeof to Check the Type of a Variable

enlightened 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).

angelThe solution → console.log(typeof seven); and console.log(typeof three);


Catch Misspelled Variable and Function Names

enlightenedThe 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.

angelThe solution → correct spelling at: receivables and payables


You can also watch the video where I talk and code the next 4 debug challenges.

Debug, Part 2/3


Catch Unclosed Parentheses, Brackets, Braces and Quotes

enlightenedA 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.

angelThe solution →

line 1: ]

line 2: )


Catch Mixed Usage of Single and Double Quotes

enlightenedJavaScript 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.

angelThe solution → single quotes, ‘#Home’


Catch Use of Assignment Operator Instead of Equality Operator

enlightenedBranching 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).

angelThe solution → line 5: if(x==y)


Catch Missing Open and Closing Parenthesis After a Function Call

enlightenedWhen 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.

angelThe solution → line 7: getNine()


You can also watch the last part of the video series where I talk and code the debug challenges.

Debug, part 3/3


Catch Arguments Passed in the Wrong Order When Calling a Function

enlightenedYou 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.

angelThe solution → line 7: raiseToPower(base, exp)


Catch Off By One Errors When Using Indexing

enlightenedOff 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.

angelThe solution → line 5: for (let i=0; i<len; i++)


Use Caution When Reinitializing Variables Inside a Loop

enlightenedSometimes 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.

angel The solution → 

line 14: row=[]

return newArray


Prevent Infinite Loops with a Valid Terminal Condition

enlightened 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.

angel 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