Pseudocode & Comments
What is pseudocode?
Pseudocode is a draft for coding, written in plain language to outline how a program should work. It's a tool that transcends specific programming languages, allowing developers to focus on the logic of their solution rather than syntax details. This approach not only aids in understanding and solving problems more effectively but also serves as a communication bridge among developers with different coding backgrounds.
In essence, it's a simpler version of programming code - in plain English.
Why use pseudocode?
It lets us focus on solving the problem instead of getting stuck on details like coding rules, the way we write code, or the programming language and tools we're using.
Using pseudocode helps us think through problems logically, keeping our attention on the issue at hand. It's great for talking things out and sharing ideas. By planning with pseudocode, we can spot potential hiccups or questions early on. Plus, there's no downside to using it, even if you've already got the solution figured out in your mind.
We use pseudocode when we want to:
Break down problems into smaller parts.
Sketch out solution ideas.
Discuss different approaches.
Test our initial thoughts and assumptions.
When writing pseudocode, you don't need to worry about strict coding rules. But, having a few easy guidelines can really help.
CAPITALISE key commands - (IF number >100 THEN do this).
Write one statement per line.
Use indentation and spacing.
Be specific.
Keep it simple - non techy.
FizzBuzz
FizzBuzz is a simple test used in interviews to check if someone understands basic coding. The goal is to write a program that counts from 1 to a number you choose. But, when a number is a multiple of 3, print "Fizz"; for multiples of 5, print "Buzz"; and for numbers that are both, print "FizzBuzz". It checks your ability to use loops, conditions, and simple maths in coding.
Using pseudocode to write a FizzBuzz program
Let's write the FizzBuzz program in pseudocode
Check out this live example on CodePen.
Let's explore more pseudocode and turn those ideas into real code. This approach helps us understand how to plan solutions and implement them effectively, improving both our coding and problem-solving skills.
Reading List
Keep track of which books you read and which books you want to read!
Steps:
- Create a list of objects, where each object describes a book and has properties for the title (a string), author (a string), and already read (a boolean (true/false) indicating if you read it yet).
- Iterate (go through) through the array of books. For each book, log the book title and book author like so: “The Hobbit by J.R.R. Tolkien”.
- Modify the output based on whether the book’s been read: if so, log a string like ‘You already read “The Hobbit” by J.R.R. Tolkien’, and if not, log a string like ‘You still need to read “The Hobbit” by J.R.R. Tolkien.’
Check out this live example on CodePen.
Recipe
Steps:
- Create a list of objects to hold information on your favourite recipes. It should have properties for:
- recipeTitle (a string)
- servings (a number)
- ingredients (an array/list of strings)
- directions (a string)
- List all recipes.
- Create a loop to list all the ingredients.
Check out this live example on CodePen.
FixStart
Instructions:
Create a function called fixStart. It should take a single argument, a string, and return a version where all occurrences of its first character have been replaced with ‘*’, except for the first character itself. You can assume that the string is at least one character long.
Check out this live example on CodePen.
Comments
Pseudocode is usually added as comments in existing files on websites. The way you write comments can vary between programming languages. So, while pseudocode itself doesn't follow a strict syntax, knowing how to comment in the specific language you're working with is important. Let's look at examples of how to add comments in HTML and JavaScript.
<!-- This is a single-line comment in HTML -->
<!-- This is a multi-line comment in HTML It can span multiple lines -->
// This is a single-line comment in JavaScript
/* This is a multi-line comment in JavaScript It can span multiple lines */
In JavaScript (as well as in many other programming languages), developers often use certain conventions in comments to highlight different types of notes for themselves and others who may work with the code. These conventions include marking comments with specific prefixes to denote their purpose, such as indicating something important, a warning, a question, or a task that needs to be completed (TODO). Here's how these might look: