A 4 step guide on how to approach solving coding problems
Tags: career, javascript, programming, codenewbie
Published: 2021-02-03 19:51:02

Personally, nothing else can be quite deflating than failing a "simple" coding problem. _HINT_: _This would work if we were using arrow functions._

```javascript
function multiply(a, b) {
    a * b
}

```
???????????????????????????????????????????????????????

                     

                    
<img  width="142" src="https://dev-to-uploads.s3.amazonaws.com/i/vpgzdwaw890m3jaf55tv.jpg"/>

<br/>
So I'm going to show you a 4 steps you can apply to help you in solving a coding challenge whether you're doing challenges on code wars, figuring out a feature for one of your projects or even if your going through an interview. This process should really help illustrate to your interviewer that you are a process orientated thinker.


## Step 1

**Articulate the problem in human language**


<br/>
On coding challenges the problem is is already stated, sometimes the wording lacks clarity. So what we do do?

We go ahead and rewrite the problems so that it makes the most sense to you. Read over it a few times, make sure nothing is lost in translation and if you haven't noticed, these problems are structured similarly to high-school algebra problems. Realising this can be quite comforting,  all we have to do is look at the back of the book! Just kidding, what we can take from this is knowing that there is a solution out there.

Let me give you an example of defining and articulating a problem in human language. It's about a kids counting game and it requires functionality see the example below:

```javascript

/*

A kid's fruit counting game needs functionality.
We need this program to add the number of fruits the user provided 
at any one time and display that number.If the number is not
provided, give a message that says no fruit has been added yet.

*/

```

<br/>
Right away i can put on my high-school algebra hat. I notice a special word _"is"_ which usually translates to an equal sign in math. So even though i don't have a clear picture yet on how exactly to approach this problem. I can already see little glimmers of hope.

Lets reformat this problem, it's not quite pseudo code just yet, but its phrased in a way that you can understand more easily.
<br/>

```javascript
/*

A kid's fruit counting game needs functionality.

We need this program to add the number of fruits the user provided at any one time and display that number.

If the number = not provided, give a message that says no fruit has been added yet.

*/
```

<br/>

## _Whats up with the spaces dude?_
<br/>
There already is an instantaneous translation into something that's one small, but illuminating step closer to our solution. We are getting into that positive feedback loop, but lets be real its not much, but we are getting there.
<br/>

So in the example above I broke up the problem into separate statements. Lets go line by line.
<br/>

- The first line, I don't see anything here that I can change so we can just keep on separating the statements. We are separating the statements so our minds can see each line as a distinct issue.
<br/>

- The second line, we need this problem to add the number of fruits the user provided at anyone time and display that number.
<br/>

- The third line, if the number equals not provided give a message that tells them no fruit has been added yet.
<br/>

Nice! So i know it seems small but it can be a real confidence booster getting in that positive feedback loop, reminding you that the translation to a computer readable solution is already in play. But its still in English. Once you've done this it naturally oozes into step 2 which is......
<br/>


## Step 2

**Iterate and translate the problem**
<br/>

Like any other programming tasks you will find yourself going over and over things, reading it, assessing it, absorbing it. Each time you do this your brain hurts a little more, but that is the feeling of thinking my friends! This cycle is commonly known as an iteration .

Now its time to translate this problem while your iterating. Don't let your brain energy go to waste just by looping through the problem and not getting anywhere,like a car spinning its tires, take action!

For each iteration, let your human problem start morphing into a programming language solution. The morphing of this language is called **pseudo-code**, it resembles a programming language but its designed for you to use more human readable terms to solve your programming problem. So lets iterate over our program again. I'm going to highlight some keywords that can help us along the way. Here's our problems translated from "human language" to pseudo-code.
<br/>

```javascript


// A kid's fruit counting game needs FUNCTIONality.

function AddFruitNumber(any number of arguments go here, including none)

// We need this program to add the number of fruits the user //provided at any one time and display that number.

add nums provided by the user, then print(total number of fruits)

// If the number = not provided, give a message that says no //fruit has been added yet.

if number = undefined, print("No fruits added...gimme fruits!")


```


<br/>
Ok cool. What is convenient about pseudo-code is that there is no compiler, or standard. In this case not having a standard works well for you, because there is no one to tell you "Hey you forgot to indent on line 14. Pseudo-code can't break and its totally up to you how you form it. Essentially the point of pseudo-code is to give your brain a step 1 in translating your problems into code. So now that we have our pseudo-code forward and on wards!
<br/>

## Step 3

**Solve it with a programming language**
<br/>
The moment we have all been waiting for, solving the program in the language of your choice. We are going to go over our pseudo-code and hopefully by this point your brain is making some connections as to how this should look as a real program.


I'm going to take what I know from JavaScript and applying to to complete the problem so I have minimum functionality, at this moment, all I want to do is get this thing to work, we can always refactor later. In the function below, I'm going to use JavaScript's argument object for now so we can have an indefinite number of arguments, this way we will make it as broad as possible, we just want to prove that it works.


```javascript

function addFruits() {
    let basketOfFruit = 0
    for(let i = 0; i < arguments.length; i++) {
      basketOfFruit +=arguments[i]
  }
  if(isNan(basketOfFruit) || basketOfFruit === 0) {
      return "No fruits added...gimme fruits!"
  } else {
      return basketOfFruits + " fruits have been added"
  }
}


```

<br/>
You might be facing an issue if you are new to programming or if you don't have a lot of experience with a specific programming language, its possible this step might stop you dead in your tracks or slow you down to a crawl. If you reach this point you're probably feeling defeated, reflecting on another career change, a cloud of negative thoughts start entering your mind, these thoughts are natural but you don't have to listen to them. So here's an extra step that can help you especially if you are going through an interview process.

## Step 3.5
<br/>
**Ask for Help: The Easter Egg Step**

Programming is a collaborative industry, actually good code is the result of a focused and collaborative effort. If you're going through an interview, ask your interviewer if they can help. Describe to them what you are trying to achieve, this way they can see your on the right path towards success and all you really need is a nudge in the correct direction. Be sure to actively listen to their feedback. Most of this stuff can be second nature for them, but not for you, they could be speaking quickly so make sure your ears are open wide.


## Step 4
<br/>
**Improve your working solution**

Solving a coding challenge is great and all, but solving it optimally is best! So we are going to refactor our solution.

Most of the time your first solution isn't the most efficient and what gets most programmers paid is, efficiency, efficiency of communication, efficiency of action, of programs. Refactoring is the technical term for redesigning our program to run better or more efficiently without changing its functionality.

So a good way to kick off this process is to ask yourself, how else can i solve this? What are some alternative ways?
Don't be afraid to experiment and break things, you already have a working solution so keep a copy of it and experiment away, let see the refactored code below and i'll explain later :


```javascript

function userAddsFruits() {
    let userBasketOfFruit = 0
    for(let i = 0; i < arguments.length; i++) {
      userBasketOfFruit +=arguments[i]
  }
  if(isNaN(userBasketOfFruit) || userBasketOfFruit === 0) {
      return "No fruits added...gimme fruits!"
  } else {
      return userBasketOfFruit + " fruits have been added"
  }
}


```

<br/>
Lets address something here. I didn't change much, lets be honest. I just renamed the function and the variables inside it, the reason I did was to make things more semantic. You really have to think of others :heart:. Changing variable  to something more meaningful increases readability of your code, try to make variables as clear as possible just in case another programmer wants to implement a feature or fix a bug in the future.

<br/>
Another thing to mention, for demonstrations sake, I did not use ES6 arrow syntax due to it not having the arguments object available but i will refactor this ES6 style just for you anyways to wrap up.
<br/>
```javascript

const userAddsFruit = (...args) => {
  let userBasketOfFruit = 0;
  args.forEach((fruit) => (userBasketOfFruit += fruit));
  if (isNaN(userBasketOfFruit) || userBasketOfFruit === 0) {
    return "No fruits added...gimme fruits!";
  } else {
    return userBasketOfFruit + " fruits have been added";
  }
};

```
<br/>
<br/>
## Not bad at all :muscle:
<br/>
<img  width="300px" src="https://dev-to-uploads.s3.amazonaws.com/i/q3xzgpx9n86pyoh93rwr.jpg"/>
<br/>


##Conclusion
<br/>
If your going through an interview and they throw a problem here are some supplementary tips if I haven't mentioned them already:
<br/>

- Think out loud.(display your though process)
- Write comments. (helps to show what your thinking as well)
- Write semantic variables, make sure they mean something.
- Code style is important, keep it consistent.
- If you get stuck ask questions, be curious.
- If you can't translate your code solution to code and are unable to solve it. Explain to your interviewer how you would've solved it in "human language". It's better than nothing.

<br/>
Thanks for making it this far! I used a simple problem to illustrate my point but this process can be applied to any problem you face in the dungeon that is a coding problem. All the best! Happy Coding!