Pseudocode

Learning Objectives

Understand what pseudocode is

Learn why we use pseudocode

Practice using pseudocode in class

What is Pseudocode?

There is no set definition

Code that is written in human-friendly form

An informal way to express the design of a computer program or an algorithm

What is Pseudocode?

Should be easy to understand

Has no standard, and everyone writes it differently

Is language ambiguous

Why use Pseudocode?

Allows us to focus on the logic of the problem we are trying to solve

Helps us think about and plan programs in a more tangible way without really writing code

Takes away the baggage of syntax and details

Example:

function (string)

testString = string

testString to array, reverse, to string

string === testString?
 

Problem: Find out if a string is a palindrome 

var palidromeTest = function(str) {
    var testString = str;
    testString = testString.split("");
    testSttring = testString.reverse();
    testString = testString.join("");
    if (testString ==== str) {
        return true;

    } else {
        return false;
    }
};

The Same Problem Written with Actual Code

Example:

1. function takes in an array

2. iterate over array

3. if element is an odd number, then print it


 

Problem: Print Each Odd Number in an Array

var oddsOnly = function(arr) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] % 2 !== 0) {

            console.log(arr[i]);

        } else {

            // do not print

        }

     } 

};

The Same Problem Written with Actual Code

We'll now spend some time practicing writing pseudo code together

Exercises

Find a partner to work with 

For each exercise, talk with each other about the different ways to solve the problem

Use markers to write out a solution in pseudocode

Given an array of numbers, create a new array with only the even numbers

Problem 1

Given a string, first find the number of words in the string, then print the string that number of times

Problem 2

Given an array of numbers, return the largest number in the array

Problem 3

Write a loop that makes seven calls to console.log to output the following triangle:

Problem 4

#
##
###
####
#####
######
#######

Write a program that prints the numbers from 1 to 100.

 

For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”.

 

For numbers which are multiples of both three and five print “FizzBuzz”

Problem 5

Pseudocode

By Jared Murphy

Pseudocode

  • 273