You've recently learned how to use pseudocode to help you plan your approach to a solution. At this point, you might be wondering what to do with that pseudocode after you've completed a problem.
In this article, you'll learn how to use comments in your code as you move through the final steps of Polya's Problem Solving Framework. Specifically, you will learn how to use comments to walkthrough, debug, and optimize a solution.
To check whether the code that you wrote behaves as intended, it's important to walk through the code with a few potential inputs.
You should use comments in your code to track key variables as you step through the code, line by line.
For example, you could use comments to track the variables in the function below with an example input. Let's walkthrough the code with the example input to see what the code is doing:
function checkIfLastCharIsAnE(str) { // str = 'ParSE'
let lastChar = str[str.length - 1]; // lastChar = 'E'
lastChar = lastChar.toLowerCase(); // lastChar = 'e'
return lastChar === 'e'; // lastChar === 'e' => true
}
checkIfLastCharIsAnE('ParSE'); //=> true
Walking through the code again using another example input:
function checkIfLastCharIsAnE(str) { // str = 'Spear'
let lastChar = str[str.length - 1]; // lastChar = 'r'
lastChar = lastChar.toLowerCase(); // lastChar = 'r'
return lastChar === 'e'; // lastChar === 'r' => false
}
checkIfLastCharIsAnE('Spear'); //=> false
This skill is really useful for making sure that the code that you wrote is doing what you intended for it to do. It also helps to see how the language interpreter will replace and process the variables with actual values.
To understand what someone else's code is doing, it's important to walk through the code line by line and understand what each line of code is doing.
You should use comments as pseudocode in the code that you are trying to understand. Pseudocode each line of code.
Let's walkthrough the code with the example input to see what the code is doing:
// Define a function called getLastThreeChars that has one string parameter
function getLastThreeChars(str) {
// Find the last character of str
let lastChar = str[str.length - 1];
// Find the second to last character of str
let secondToLastChar = str[str.length - 2];
// Find the third to last character of str
let thirdToLastChar = str[str.length - 3];
// Concatenate the third to last, second to last, and last characters of str
// and return the concatenated string
return thirdToLastChar + secondToLastChar + lastChar;
}
After adding pseudocode for each line of code, apply the same process for walking through your own code with comments by tracking the variables with an example input:
// Define a function called getLastThreeChars that has one string parameter
function getLastThreeChars(str) { // str = 'last'
// Find the last character of str
let lastChar = str[str.length - 1]; // lastChar = 't'
// Find the second to last character of str
let secondToLastChar = str[str.length - 2]; // secondToLastChar = 's'
// Find the third to last character of str
let thirdToLastChar = str[str.length - 3]; // thirdToLastChar = 'a'
// Concatenate the third to last, second to last, and last characters of str
// and return the concatenated string
return thirdToLastChar + secondToLastChar + lastChar; // 'a' + 's' + 't' => 'ast'
}
getLastThreeChars("last"); //=> 'ast'
Debugging is the process of finding and fixing errors or bugs in code. To improve your problem solving skills, you should try reading and understanding someone else's code and solve the errors or bugs in the code.
Apply the process of adding pseudocode to someone else's code and tracking the variables with an example input to figure out where the bug or error is in the code. Then fix the code.
The code in the previous example now has a bug. Try using the processes you just learned to spot where the bug in the code is:
function getLastThreeChars(str) {
let lastChar = str[str.length - 1];
let secondToLastChar = str[str.length - 3];
let thirdToLastChar = str[str.length - 3];
return thirdToLastChar + secondToLastChar + lastChar;
}
getLastThreeChars("last"); // should evaluate to 'ast'
Did you try using the processes? Did you spot the bug?
Here's an example of someone's attempt at pseudocode and replacing of variables:
// Define a function called getLastThreeChars that has one string parameter
function getLastThreeChars(str) { // str = 'last'
// Find the last character of str
let lastChar = str[str.length - 1]; // lastChar = 't'
// Find the second to last character of str
let secondToLastChar = str[str.length - 2]; // secondToLastChar = 's'
// Find the third to last character of str
let thirdToLastChar = str[str.length - 3]; // thirdToLastChar = 'a'
// Concatenate the third to last, second to last, and last characters of str
// and return the concatenated string
return secondToLastChar + thirdToLastChar + lastChar; // 's' + 'a' + 't' => 'sat'
}
getLastThreeChars("last"); //=> 'sat'
The pseudocode that the person wrote didn't spot the bug. But they were able to get a clue into where the bug could be. The code is supposed to return 'ast' but when replacing the variables, the code looks like it returns 'sat' instead. It seems like an issue with the last line of code in the function code block: return secondToLastChar + thirdToLastChar + lastChar;.
Now, it should be easy to identify and fix the code to make it work properly!
Including comments in your code is an important learning tool in reading and debugging code. Pseudocode can be used to help plan your code and to document other people's code. Replacing variables in comments can also be used to verify that your code is working as intended and to debug code.