31

# 5 Steps to Solving Programming Problems

We solve issues all the time as people, and developers are no different. Problem-solving-focused classes aren't particularly popular or frequent, and many developers prefer to study tools, languages, and frameworks over learning how to think like a problem solver or a programmer.

Problem solving is a programmer's bread and butter, and while everyone has their own technique, I've discovered five methods that will most certainly help you not only solve issues faster and more efficiently.

Problem-solving can mean different things for different people or situations so it is good to clarify what this article means when “problem-solving” is mentioned.

When you bring your broken automobile to the shop, they may decide to fix what's broken, replace the broken part, or offer you the option of purchasing a new car. Even though all of these alternatives appear to be “solutions,” only the first one truly addresses the issue. Everything else is an attempt to avoid dealing with the issue.

You solve a problem when given a set of constraints and having to follow some rules you come up with a solution that meets all the constraints and does not break the rules. As programmers, we write a program, a set of instructions that solves the problem.

Anyone who invests the effort to learn how to code will eventually be able to program. It's similar to learning a new language to learn to code. It is the ability to provide instructions for a computer to follow using a language that can be understood or compiled by a computer.

Problem-solving is a separate skill set, and we are inherently adept at it as humans. I mean, by solving problem after problem, we constructed the world around us. Connecting these two skill sets is something that many developers struggle with. Solving programming issues improves your ability to solve real-world problems, and if you're excellent at them, programming may come easy to you.

Problem-solving is a separate skill set, and we are inherently adept at it as humans. I mean, by solving problem after problem, we constructed the world around us. Connecting these two skill sets is something that many developers struggle with. Solving programming issues improves your ability to solve real-world problems, and if you're excellent at them, programming may come easy to you.

Let’s pretend we are creating a simple function `selectEvenNumbers`

that will take in an array of numbers and return an array `evenNumbers`

of only even numbers. If there are no even numbers, return the empty array `evenNumbers`

.

```
function selectEvenNumbers() {
// your code here
}
```

Here are some questions that run through my mind:

- How can a computer tell what is an even number? Divide that number by 2 and see if its remainder is 0.
- What am I passing into this function? An array
- What will that array contain? One or more numbers
- What are the data types of the elements in the array? Numbers
- What is the goal of this function? What am I returning at the end of this function? The goal is to take all the even numbers and return them in an array. If there are no even numbers, return an empty array.

Take out a piece of paper and work through the problem manually. Think of at least three sets of sample data you can use. Consider corner and edge cases as well.

Corner case: a problem or situation that occurs outside of normal operating parameters, specifically when multiple environmental variables or conditions are simultaneously at extreme levels, even though each parameter is within the specified range for that parameter.

Edge case: problem or situation that occurs only at an extreme (maximum or minimum) operating parameter

For example, here are some sample data sets to use:

```
[1]
[1, 2]
[1, 2, 3, 4, 5, 6]
[-200.25]
[-800.1, 2000, 3.1, -1000.25, 42, 600]
```

Because your brain is already accustomed with even numbers, you may easily glance at a sample set of data and pluck out numbers like `2`

,`4`

,`6`

, and so on in the array without realizing it. If you're having trouble, consider using massive quantities of data, which will overcome your brain's natural ability to answer the problem simply by looking at it.That helps you work through the real algorithm.

Let’s go through the first array `[1]`

- Look at the only element in the array
`[1]`

- Decide if it is even. It is not
- Notice that there are no more elements in this array
- Determine there are no even numbers in this provided array
- Return an empty array

Let’s go through the array `[1, 2]`

1.Look at the first element in array `[1, 2]`

- It is
`1`

- Decide if it is even. It is not
- Look at the next element in the array
- It is
`2`

- Decide if it is even. It is even
- Make an array
`evenNumbers`

and add`2`

to this array - Notice that there are no more elements in this array
- Return the array evenNumbers which is
`[2]`

I go through this a few more times. Notice how the steps I wrote down for `[1]`

varies slightly from `[1, 2]`

. That is why I try to go through a couple of different sets. I have some sets with just one element, some with floats instead of just integers, some with multiple digits in an element, and some with negatives just to be safe.

Look for patterns and see if there’s anything you can generalize. See if you can reduce any steps or if you are repeating any steps.

1.Create a function `selectEvenNumbers`

- Create a new empty array
`evenNumbers`

where I store even numbers, if any - Go through each element in the array
`[1, 2]`

- Find the first element
- Decide if it is even by seeing if it is divisible by 2. If it is even, I add that to
`evenNumbers`

- Find the next element
- Repeat step #4
- Repeat step #5 and #4 until there are no more elements in this array
- Return the array
`evenNumbers`

, regardless of whether it has anything in it

This approach may remind you of Mathematical Induction in that you:

1.Show it is true for n = 1, n = 2, ...

2.Suppose it is true for n = k

3.Prove it is true for n = k + 1

Even once you've figured out the main processes, developing pseudocode that you can translate into code can help you define your code's structure and make coding a lot easier. Line by line, write pseudocode. You may do this on paper or in your code editor as comments. I recommend doing it on paper if you're just starting off and find blank displays intimidating or distracting.

Pseudocode generally does not actually have specific rules in particular but sometimes, I might end up including some syntax from a language just because I am familiar enough with an aspect of the programming language. Don’t get caught up with the syntax. Focus on the logic and steps.

Let's think about the steps needed to write a function that returns a number's squared value.

```
// Initialize a variable with a 'n' value
// Multiply variable by it self
// Return the result of that multiplication
```

Now we know exactly what our code is supposed to do, we have one more step.

When you have your pseudocode ready, translate each line into real code in the language you are working on. We will use JavaScript for this example.

If you wrote it out on paper, type this up as comments in your code editor. Then replace each line in your pseudocode.

Lets use our square example (very simple for demonstration purposes):

```
function square(n) {
// Initialize a variable with a 'n' value
const initialValue = n
// Multiply variable by it self
const squaredValue = initialValue * initialValue
// Return the result of that multiplication
return squaredValue
}
```

Optimize your code:

```
function square(n) {
return n * n
}
```

If you've reached this point, thank you very much. I hope that this tutorial has been helpful for you and I'll see you all in the next.

If you want to learn more about Web Development don't forget to follow me on Youtube!

31