Introduction to Pseudocode

At this point in the course, you've started encountering problems that are more challenging for you. You might read through the problem and have a good idea about how to approach the problem logically, but maybe you're not quite sure how to translate that logical plan into working code.

In this reading, you'll learn how to use pseudocode to help bridge the gap between your logical plan and your code implementation. You will learn:

What is Pseudocode?

Pseudocode can be thought of as a "fake" version of your code. When you write pseudocode, you are writing a plan for a solution that includes the logic, but not necessarily the syntax or methods. For example, let's say you are given the following problem:

/*
Given a string, write a function to return true if the string's last character
is either an 'E' or a 'e'.
*/
    

You can start by using pseudocode and the steps of Polya's Problem Solving framework.

1. Understand the Problem

You can type out a few short comments summarizing your understanding of the problem. For example, you can jot down some details about the problem, the data types involved, and any potential edge cases you might have to account for.

/*
Given a string, write a function to return true if the string's last character
is either an 'E' or a 'e'.
*/
// goal: return a boolean of whether the input string's last character is 'E' or 'e'
// input: string
// output: true or false (boolean)
// cases:
// 1. a string ending in 'e'
// 2. a string ending in 'E'
// 3. a string ending in a lower-cased character that isn't 'e' or 'E'
// 4. a string ending in a upper-cased character that isn't 'e' or 'E'
    

You've just completed step 1 of Polya, in pseudocode.

2. Make a Plan

Next, you can make a plan for solving the problem. Your pseudocode will focus on the logic you will use to solve the problem, and will not focus on specific methods and syntax. It can be helpful to write out a series of steps you will take to solve the problem. However, it's also ok to get your basic ideas down first and re-order them once you have everything figured out. For example:

// 1. Define a function with the name `checkIfLastCharIsAnE`.
// 2. Define a single parameter for the function called `str`.
// 3. Find the last character of `str`.
// 4. Return true if the last character of `str` is an 'e' or if the last
//    character of `str` is an 'E'. Otherwise return false.
    

3. Carry Out the Plan

Next, you're ready to write your code. Now is the time to turn your logical plan into JavaScript methods and syntax. You can do this by typing code between the lines of your pseudocode plan. This is when you will work out the important details, such as the comparison expression necessary to work out the logic in the last step of the plan.

// 1. Define a function with the name `checkIfLastCharIsAnE`.
// 2. Define a single parameter for the function called `str`.
function checkIfLastCharIsAnE(str) {
    // 3. Find the last character of `str`.
    let lastChar = str[str.length - 1];
    // 4. Return true if the last character of `str` is an 'e' or if the last
    //    character of `str` is an 'E'. Otherwise return false.
    return lastChar === 'e' || lastChar === 'E';
}
    

What is NOT Pseudocode?

Pseudocode is a great tool to help you break down problems, plan out a logical solution, and then implement that solution into working code. But when pseudocode is used incorrectly, it can lead to some bad habits that are difficult to correct. As you practice your pseudocode, make sure you don't write code.

Coming back to the definition, pseudocode is fake code, and it's important to keep it that way! If you find yourself starting to make comments that focus more on syntax and methods, then you are no longer writing pseudocode. This is a problem because it means you are skipping the first few steps of Polya and jumping right to a solution instead of taking the time to fully understand the problem and make a plan first.

What does this pitfall look like?

// Pitfall: Taking the "pseudo" out of pseudocode
/* ============= DON'T DO THIS!!! ============= */

// function checkIfLastCharIsAnE(string)
  // lastChar = str[lastIndex]
  // return true if lastChar === 'e'
  // return false if lastChar !== 'E'
    

What's wrong with this? It's basically just JavaScript code, in the form of comments! While the plan might be an effective one, it is dangerous because you have taken a shortcut and have probably not fully thought through the logic and brainstormed potential edge cases. This approach might work with some easier problems, but will break down when you encounter more challenging problems.

What You've Learned

In this article you've learned how to use pseudocode comments, or "fake" code, to help you move through the first three steps of Polya's Problem Solving Framework. This helps you move from a solid logical plan into working JavaScript. When writing pseudocode, it's important to make short notes focusing on the logic, and not jumping right into the code. It's also important to focus on the major steps of the solution, and not every minor detail. As you start practicing your pseudocode, you'll come up with a consistent style that works for you.