using pseudocode as a beginner - why and how
Tags: javascript, beginners, codenewbie, tutorial
Published: 2022-09-14 03:05:37

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 <u>plain English</u> 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](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for---in-statement).

**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));
```
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
```
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;
```
 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](https://www.freecodecamp.org/news/how-to-iterate-over-objects-in-javascript/)
[for...in MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)