5 Steps to Solving Programming Problems
Tags: webdev, tutorial, programming, algorithms
Published: 2021-07-27 14:54:29

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.

## What is Problem Solving?
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.


## To Code is different than To Solve 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.


## 1. Read the problem several times until you can explain it to someone else
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtx5k0fhmvw8r7lsukux.png)
You can’t solve a problem you don’t understand. There is a difference between the problem and the problem you think you are solving. It’s easy to start reading the first few lines in a problem and assume the rest of it because it’s similar to something you’ve seen in the past. If you are making even a popular game like Hangman, be sure to read through any rules even if you’ve played it before. I once was asked to make a game like Hangman that I realized was “Evil Hangman” only after I read through the instructions (it was a trick!).

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`.
```javascript
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.

## 2. Manually solve the problem with at least three sets of sample data.
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](https://en.wikipedia.org/wiki/Corner_case) and [edge cases](https://en.wikipedia.org/wiki/Edge_case) as well.

>[Corner case](https://en.wikipedia.org/wiki/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](https://en.wikipedia.org/wiki/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:
```javascript
[1]
[1, 2]
[1, 2, 3, 4, 5, 6]
[-200.25]
[-800.1, 2000, 3.1, -1000.25, 42, 600]

```
## When you are first starting out, it is easy to gloss over the steps.
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]`

1. Look at the only element in the array `[1]`
2. Decide if it is even. It is not
3. Notice that there are no more elements in this array
4. Determine there are no even numbers in this provided array
5. Return an empty array

Let’s go through the array `[1, 2]`
1.Look at the first element in array `[1, 2]`
2. It is `1`
3. Decide if it is even. It is not
4. Look at the next element in the array
5. It is `2`
6. Decide if it is even. It is even
7. Make an array `evenNumbers` and add `2` to this array
8. Notice that there are no more elements in this array
9. 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.

## 3. Simplify and optimize your steps
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`
2. Create a new empty array `evenNumbers` where I store even numbers, if any
3. Go through each element in the array `[1, 2]`
4. Find the first element
5. Decide if it is even by seeing if it is divisible by 2. If it is even, I add that to `evenNumbers`
6. Find the next element
7. Repeat step #4
8. Repeat step #5 and #4 until there are no more elements in this array
9. Return the array `evenNumbers`, regardless of whether it has anything in it

This approach may remind you of [Mathematical Induction](https://en.wikipedia.org/wiki/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

## 4.Write pseudocode
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.

## 5. Translate pseudocode into code 
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):
```javascript
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:
```javascript
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 like my work, please consider
[![Buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm11knj7d8zwcvo150q2.png)](https://www.buymeacoffee.com/cglikpo)
so that I can bring more projects, more articles for you


If you  want to learn more about Web Development don't forget to [follow me on Youtube!](https://www.youtube.com/c/ChristopherGlikpo/featured)