The PREP Method
A great way to "prep" your code before you solve coding challenges!
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:
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.
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.
Another example below also works as pseudocode:
Source: unf.edu/~broggio/cop2221/2221pseu.htm
Now, we're ready to code!
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!
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
Follow me on Twitter and join my Timeout Tuesdays: twitter.com/izaye_visuals