using pseudocode as a beginner - why and how

monlir

monica

Posted on September 14, 2022

using pseudocode as a beginner - why and how

In this post, we will discuss the importance of pseudocode, how to approach a coding problem with pseudocode, and finally, an example applying this approach.

Please note that pseudocode is not a programming or scripting language, it is simply the practice of using plain English to understand code, line by line.

Why should you take the time to pseudocode? Is it worth it?

The short answer to this is YES.

Pseudocoding allows us to list out each step in a sequential form, guiding us to a logical solution. Spending the 10-15min pseudocoding before writing code allows for a quicker and less stressful construction phase.

How do you write pseudocode?

There isn't a standardized convention for writing pseudocode. I believe this flexibility is what makes the practice of pseudocoding valuable because you can do so in a variety of algorithms and languages.

This flexibility can be daunting for some, especially beginners, so here are some guidelines to help get you to writing useful pseudocode:

  • capitalize conditional keywords
  • write one statement per line of code
  • use indentation + syntax correctly
  • be specific with variable names
  • describe each step simply, don't leave any room for assumptions

The main goal is to get your pseudocode to a place logically that allows you to easily construct code to achieve the desired output.

Let's see it in action...

In this example, we are going to be solving a JavaScript challenge from freeCodeCamp.

Iterate Through the Keys of an Object with a for...in Statement

In this challenge, we are asked to use a for...in statement within this countOnline function to loop through the users object passed into the function and return the number of users whose online property is set to true.

 const users = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

function countOnline(usersObj) {
  // Only change code below this line

  // Only change code above this line
}

console.log(countOnline(users));
Enter fullscreen mode Exit fullscreen mode

Let's write some pseudocode...

Main goal:
countOnline should use a for in statement to iterate through the object keys of the user object passed to it

FOR (let user in userObj) {
 IF (user === online) {
  THEN increase counter 
 }
 return counter
Enter fullscreen mode Exit fullscreen mode

With this pseudocode written, I now know exactly what this function is supposed to do. I now feel comfortable with my logic to write the solution in JavaScript.

let result = 0;
  for (let user in usersObj) {
    if (usersObj[user].online === true) {
      result++;
    }
  }
  return result;
Enter fullscreen mode Exit fullscreen mode

Ta-da! We successfully went through a scenario in which pseudocoding helped us logically solve a JavaScript challenge.

To wrap things up...

Learning how to write concise pseudocode allows us to work through different logical scenarios to achieve the desired result. Remember to write pseudocode for each line of code to ensure no step is missed in the solution. There are many ways to write pseudocode, but ultimately its purpose is to help you brainstorm and plan out a solution before writing code. No more staring at your text editor trying to conjure up a solution, now you can create a baseline to start from and feel confident in your work.

Happy coding!

Resources:
How to Iterate Over Objects in JavaScript
for...in MDN Documentation

💖 💪 🙅 🚩
monlir
monica

Posted on September 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Mastering the For Loop in JavaScript
javascript Mastering the For Loop in JavaScript

September 22, 2023

JS Test #3: try/catch
javascript JS Test #3: try/catch

December 18, 2022

using pseudocode as a beginner - why and how
javascript using pseudocode as a beginner - why and how

September 14, 2022

Method chaining in Javascript
javascript Method chaining in Javascript

September 13, 2022

Demystifying Fetch()
javascript Demystifying Fetch()

July 12, 2022