The PREP Method

The PREP Method

A great way to "prep" your code before you solve coding challenges!

ยท

3 min read

What exactly is the PREP Method?

PREP is a mnemonic way to problem solve your coding issues/challenges. This type of thinking can help you break down the problem logically.

Let's break down what PREP stands for...

  • P - Parameters
  • R - Return
  • E - Examples
  • P - Psuedocode

P - Parameters

The first step is to think about the input(s) of the function, the type(s) of parameter(s), and how we'll possibly name the parameter(s). Let's look at a JavaScript example from Codewars:

Screenshot 2022-10-30 at 2.52.50 PM.png

Let's first figure out what parameters are in this problem. In this example, there are 2 parameters mentioned: n and s. The "types" of parameters we need is a "number" and a "string", hence why they named it n and s.

Look at that! We just finished the first step of PREP! ๐Ÿฅณ

R - Return

The second step is to figure out what the function will return. In problem, it is asking for a "string"(s) that is repeated by n number of times.

E - Examples

Next, let's think of examples that could be returned. In this problem, they have given us a few examples.

Screenshot 2022-10-30 at 3.00.38 PM.png

What other examples can we think of? ๐Ÿ‘€

P - Pseudocode

Finally, it's time for some pseudocode. It's really up to you how you want to write your pseudocode, but essentially this is where you write what your code will do.

Screenshot 2022-10-31 at 3.20.25 PM.png

Another example below also works as pseudocode:

Screenshot 2022-10-31 at 3.21.15 PM.png

Source: unf.edu/~broggio/cop2221/2221pseu.htm

Now, we're ready to code!

Screenshot 2022-10-30 at 2.52.50 PM.png

After reading the code and setting up our PREP approach, we can tackle this problem. Since the problem is specifically targeting strings, we should think about different JavaScript string methods that can repeat strings.

This is where we can apply repeat() to our code. Here is the syntax of this particular string method: string.repeat(count).

Let's apply what we just learned and see if it works!

Screenshot 2022-10-31 at 3.33.51 PM.png

Woohoo! It works! ๐Ÿ™Œ๐Ÿผ What are other potential ways we could solve this problem? ๐Ÿค”

Conclusion

This kind of approach has helped me in so many codewars challenges and in my own projects! Once you get the hang of this tactic, challenges aren't as intimidating and big problems are then broken down into smaller chunks. Hope this brief explanation helps you out in your coding journey!

What are other ways you tackle your coding problems?






Thanks for reading my friend!

Izzy Izaye-02.png

Follow me on Twitter and join my Timeout Tuesdays: twitter.com/izaye_visuals

ย