PREP Technique for Algorithms
Tags: javascript
Published: 2020-09-18 23:22:40

## What is PREP?
PREP is a methodology in which to approach an algorithm or coding challenge. The acronym stands for Parameters, Return, Example, Pseudocode.

* **Parameters**
What is the type of argument being inputted and are there multiple or default arguments?

* **Return**
What is being asked to be returned and what is the type?

* **Example**
Provide 2-3 examples of what we expect to happen if we call the function with certain arguments. Can you think of any edge cases?

* **Pseudocode**
Write informal steps on how to solve the problem. Think about what the parameters are and what steps need to be taken to get to our expected return. All statements showing "dependency" are to be indented.

## Examples

### **reverse()**
_Given a string, return a new string with the reversed order of characters._

* **P** - Single string argument
* **R** - A string that is the reverse of the original string argument
* **E** - Account for spaces, capitalizion, and punctuation marks 
```javascript
// reverse('hello world') --> 'dlrow olleh'
// reverse('Love!') --> '!evoL'
// reverse('JavaScript') --> 'tpircSavaJ'
```
* **P** - see below
```javascript
function reverse(str) {
  // With keyword let, declare & assign a variable reverseStr to ''
  
  // Iterate over str from the last idx to the 0th idx
    // Add the character at the current idx to reverseStr
  
  // return reverseStr

}
```

Now, translate pseudocode procedure to code.

#### Solution
```javascript
function reverse(str) {
  // With keyword let, declare & assign a variable reverseStr to ''
  let reverseStr = ''
  // Iterate over str from the last idx to the 0th idx
    // Add the character at the current idx to reverseStr
  for (let i=str.length-1; i>=0; i--) {
    reverseStr+=str[i]
  }
  // return reverseStr
  return reverseStr
}
```

### **capitalize()**

_Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string._

* **P** - Single string argument
* **R** - A string where the first character of each word of the original string argument is capitalized
* **E** - Account for spaces and punctuation marks

```javascript
//   capitalize('hello world!') --> 'Hello World!'
//   capitalize('i love code') --> 'I Love Code'
//   capitalize('one, two, three...') --> 'One, Two, Three..'
```
* **P** - see below
```javascript
function capitalize(str) {
  // Split the str argument by the spaces ' ', creating an array of words

  // With the array of words, use map method to capitalize each word's first element at index 0 & join it to the remaining word's letters

  // Rejoin the words by a space ' '

  // Wrap the code above in a return

}
```

Now, translate pseudocode procedure to code.

```javascript
function capitalize(str) {
  // Split the str argument by the spaces ' ', creating an array of words
  str.split(' ')
  // With the array of words, use map method to capitalize each word's first element at index 0 & join it to the remaining word's letters
  .map(word => word[0].toUpperCase() + word.slice(1))
  // Rejoin the words by a space ' '
  .join(' ')
  // Wrap the code above in a return
  
}
```

For our last step, we'll need to bring the return keyword to the top of the code block.

#### Solution
```javascript
function capitalize(str) {
  return str.split(' ')
            .map(word => word[0].toUpperCase() + word.slice(1))
            .join(' ')
}
```

## Conclusion

When solving an algorithm, remember to think of your **parameters** and **return** _immediately_. If **examples** are not provided, write out a few yourself so that you may test as you go. **Pseudocode** your thoughts on how to solve and keep it in a syntax in which can be converted to code.

& Remember... Happy coding, friends! =)