Marcy Lab School Docs
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • How-Tos
    • How To Code at Marcy: Code Style Guide
    • How to Do Short Response and Coding Assignments
    • How to Debug
    • How to PEDAC
    • How to Create A GitHub Organization and Scrumboard
    • How to Create Projects with Vite
    • How to Deploy on GitHub Pages
    • How to Deploy on Render
    • How to Test your API with Postman
  • Mod 0 - Command Line Interfaces, Git, and GitHub
    • Overview
    • 1. Command Line Interfaces
    • 2. Git & GitHub
    • 3. Git Pulling & Merging
    • 4. Git Branching & PRs
  • Mod 1 - JavaScript Fundamentals
    • Overview
    • 1. Intro to Programming
    • 2. Errors
    • 3. Node & Node Modules
    • 4. Variables, Functions & String Methods
    • 5. Control Flow, typeof, and Math
    • 6. Loops
    • 7. Arrays
    • 8. Objects
    • 9. Higher Order Functions: Callbacks
    • 10. Higher Order Functions: Array Methods
    • 11. Regex
  • Mod 2 - HTML, CSS & the DOM
    • Overview
    • 1. HTML
    • 2. CSS
    • 3. Accessibility (a11y)
    • 4. The Document Object Model (DOM) API
    • 5. Events
    • 6. Forms
    • 7. The Box Model and Positioning
    • 8. Flexbox
    • 9. Grid & Media Queries
    • 10. ESModules
    • 11. Vite
    • 12. LocalStorage
  • Mod 3 - Async & APIs
    • Overview
    • 1. Promises
    • 2. Fetch
    • 3. Building a Fetching App
    • 4. Async & Await
    • 5. A Generic Fetch Handler
  • Mod 4 - Project Week!
    • Important How Tos and Guides
      • How to Create a GitHub Organization and Scrum Board
      • How To Start a Project with Vite
      • How To Deploy a Project with GitHub Pages
    • Project Week Overview
    • Agile Methodologies
    • Deliverables & Milestones
    • Technical Requirements Checklist
    • Free API List
    • Collaborative GitHub
  • Mod 5 - Object-Oriented Programming
    • Overview
    • 1. Intro to OOP, Encapsulation, Factory Functions, and Closure
    • 2. Classes
    • 3. Private & Static
    • 4. UML Diagrams & Has Many/Belongs To Relationships
    • 5. Challenge: Implementing Has Many/Belongs To
    • 6. Inheritance
    • 7. Polymorphism
    • 8. Review and Practice
    • MDN: Object Prototypes
  • Mod 6 - Data Structures & Algorithms
    • Overview
    • Important How Tos and Guides
      • How to Debug
      • How to PEDAC
    • 1. Nodes & Linked Lists
    • 2. Singly & Doubly Linked Lists
    • 3. Stacks & Queues
    • 4. Recursion
    • 5. Trees
  • Mod 7 - React
    • Overview
    • Important How Tos and Guides
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
    • 1. Intro to React
    • 2. Events, State, and Forms
    • 3. Fetching with useEffect
    • 4. React Router
    • 5. Building a Flashcards App
    • 6. React Context
    • 7. Global Context Pattern
  • Mod 8 - Backend
    • Overview
    • Important How Tos and Guides
      • How to Deploy on Render
      • How to Test your API with Postman
      • Postgres Setup
    • 1. Intro to Express
    • 2. Building a Static Web Server with Middleware
    • 3. Securing API Keys and Environment Variables
    • 4. RESTful CRUD API
    • 5. Model-View-Controller Architecture
    • 6. SQL and Databases
    • 7. JOIN (Association) SQL Queries
    • 8. Knex
    • 9. Your First Fullstack App!
    • 10. Migrations & Seeds
    • 11. Schema Design & Normalization
    • 12. Hashing Passwords with Bcrypt
    • 13. React Express Auth Template Overview
  • Mod 9 - Civic Tech Hackathon
    • Overview
    • Rubric
  • Mod 10 - Capstone
    • Overview
Powered by GitBook
On this page
  • PEDAC Problem Solving
  • Example PEDAC
  • Tips for Making Your Own PEDAC
  • P — State the Problem
  • E — Examples / Test Cases
  • D — Data Structures and Tools
  • A — Algorithm
  • C — Code!
  1. How-Tos

How to PEDAC

PreviousHow to DebugNextHow to Create A GitHub Organization and Scrumboard

Last updated 7 months ago

Table of Contents:

PEDAC Problem Solving

Imagine that you had the following problem to solve:

Consider this array.

const users = [
  { id: 1, name: 'ben', isAdmin: false},
  { id: 2, name: 'maya', isAdmin: true},
  { id: 3, name: 'reuben', isAdmin: true},
  { id: 4, name: 'gonzalo', isAdmin: false},
  { id: 5, name: 'ana', isAdmin: true},
]

Write a function getAdminIds that has one parameter, an array users full of objects that have an id, name, and isAdmin properties. The function should return an array containing the id numbers of the users where the isAdmin property is true. If none are found, return an empty array. If the users array is not an array, return null. Do not modify the original array.

You might have an idea of how to solve the problem, but before you do, try planning out your approach first! Doing so has a number of benefits:

  • You can work on the skill of problem solving separately from the skill of coding!

  • You will have a roadmap for writing a solution that you can return to if you get lost while coding.

  • If you are having trouble implementing your solution, you can bring your planning to an instructor, mentor, or peer to quickly get them up to speed on the problem.

At Marcy, we use PEDAC which is an acronym that stands for the following problem solving strategy:

  • State the Problem

  • Think of Examples and edge cases

  • List the Data Structures and tools you can use

  • Plan out the Algorithm

  • Code the solution!

Example PEDAC

Here is an example of our PEDAC process:

/* 
PEDAC

State the Problem
- inputs: 
  - users: an array of objects, each with the properties id, name, and isAdmin

- output:
  - an array of numbers representing the id numbers of the admins in users

Examples / Test Cases:

const users = [
  { id: 1, name: 'ben', isAdmin: false},
  { id: 2, name: 'maya', isAdmin: true},
  { id: 3, name: 'reuben', isAdmin: true},
  { id: 4, name: 'gonzalo', isAdmin: false},
  { id: 5, name: 'ana', isAdmin: true},
]

getAdminIds(users) -> [2, 3, 5]
getAdminIds([{ id: 1, name: 'ben', isAdmin: false}]) -> []
getAdminIds([{ id: 1, name: 'ben', isAdmin: true}]) -> [1]
getAdminIds([]) -> []
getUserById() -> null
getUserById(5) -> null

Data Structures & Tools
- an array to hold the ids of the admins (this is the return value)
- for loop to iterate through the array
- bracket notation to access values in the array
- dot notation to access object properties
- if statements to check if an object is an admin

Algorithm
- Set `ids` to be a new array to store the ids of admins
- Iterate through the input `users` array. For each object...
  - If the current object is an admin...
  - Add that object's id to the `ids` array
- Return the `ids` array after the iteration is complete.
*/

const getAdminIds = (users) => {
  const ids = [];
  for (let i = 0; i < users.length; i++) {
    const currentUser = users[i];
    if (currentUser.isAdmin) {
      ids.push(currentUser.id)
    }
  }
  return ids;
}

// Test Code

const users = [
  { id: 1, name: 'ben', isAdmin: false},
  { id: 2, name: 'maya', isAdmin: true},
  { id: 3, name: 'reuben', isAdmin: true},
  { id: 4, name: 'gonzalo', isAdmin: false},
  { id: 5, name: 'ana', isAdmin: true},
]

console.log(getAdminIds(users)); // -> [2, 3, 5] because three admins with these ids were found
console.log(getAdminIds([{ id: 1, name: 'ben', isAdmin: false}])); // -> [] because no admins were found
console.log(getAdminIds([{ id: 1, name: 'ben', isAdmin: true}])); // -> [1] because only 1 admin was found
console.log(getAdminIds([])); // -> [] because no admins were found
console.log(getUserById()); // -> null because the input was undefined
console.log(getUserById(5)); // -> null because an array was not provided

Tips for Making Your Own PEDAC

Let's look at how we arrived at the PEDAC above.

P — State the Problem

Before you can solve a problem, you must fully understand the problem. When you have a problem in front of you, start by listing inputs and outputs

Tip: Write this planning in a comment directly in your code!

When listing inputs and outputs, you want to be as descriptive as possible.

  • Rather than "an array" say "an array of numbers"

  • Rather than "an object" say "an object with the properties x, y, and z"

  • Rather than "an array of objects" say "an array of objects, each with the properties x, y, and z"

/* 
PEDAC

State the Problem
- inputs: 
  - users: an array of objects, each with the properties id, name, and isAdmin

- output:
  - an array of numbers representing the id numbers of the admins in users

E — Examples / Test Cases

When listing examples, imagine you are invoking your own function. Come up with a few different examples with "good" inputs, a few with "bad" inputs, and a few with undefined inputs. For each example, include the expected result of calling the function.

/*
Examples / Test Cases:

getAdminIds(users) -> [2, 3, 5]
getAdminIds([{ id: 1, name: 'ben', isAdmin: false}]) -> []
getAdminIds([{ id: 1, name: 'ben', isAdmin: true}]) -> [1]
getAdminIds([]) -> []
getUserById() -> null
getUserById(5) -> null

D — Data Structures and Tools

List all of the relevant tools that you might use to solve the problem, including:

  • Data you need to store in variables

    • Primitive data values: strings, numbers, booleans to represent x, y or z

    • Reference data values: arrays or objects to hold x, y, and z

  • Control Flow Constructs

    • for or while loops to repeat x or to iterate through y

    • if/else if/else statements to determine whether to x, y, or z

  • Global Functions / Methods

    • Array methods

    • String methods

    • Math methods

    • etc...

/* 
Data Structures & Tools
- an array to hold the ids of the admins (this is the return value)
- for loop to iterate through the array
- bracket notation to access values in the array
- dot notation to access object properties
- if statements to check if an object is an admin

A — Algorithm

Plan out the order of operations to process the inputs and return the desired output. During this stage, you should be writing "pseudocode". Pseudocode should be written such a way that anyone with basic programming knowledge could implement your algorithm in any language.

/* 
Algorithm
- Set `ids` to be a new array to store the ids of admins
- Iterate through the input `users` array. For each object...
  - If the current object is an admin...
  - Add that object's id to the `ids` array
- Return the `ids` array after the iteration is complete.
*/

C — Code!

Once you've completed the first four steps, go ahead and build your solution! Use your examples to test your solution.

const getAdminIds = (users) => {
  const ids = [];
  for (let i = 0; i < users.length; i++) {
    const currentUser = users[i];
    if (currentUser.isAdmin) {
      ids.push(currentUser.id)
    }
  }
  return ids;
}

console.log(getAdminIds(users)); // -> [2, 3, 5]
console.log(getAdminIds([{ id: 1, name: 'ben', isAdmin: false}])); // -> []
console.log(getAdminIds([{ id: 1, name: 'ben', isAdmin: true}])); // -> [1]
console.log(getAdminIds([])); // -> []
console.log(getUserById()); // -> null
console.log(getUserById(5)); // -> null
PEDAC Problem Solving
Example PEDAC
Tips for Making Your Own PEDAC
P — State the Problem
E — Examples / Test Cases
D — Data Structures and Tools
A — Algorithm
C — Code!