Be a Problem Solver Challenge!
Tags: productivity, challenge, javascript, beginners
Published: 2020-02-21 18:10:39

Challenge yourself to solve problems you've never seen before with this step-by-step guide to being a programming problem solver!

## 1. Choose a problem to solve. 

Try Codewars.com or Google 'coding challenges'. 

I had the opportunity to engage in the problem-solving process with a partner as a student at Turing School of Software Development and Design in Denver. If you can work with a partner, do it! Two brains are better than one. I learn the most when I can talk through my ideas and hear how someone else thinks about the same problem. If you don't have a partner, talk to yourself. Seriously, it will develop your understanding and vocabulary if you talk through a problem before you attempt to program it.

Our instructor gave us some challenges to choose from and we chose Counting Sheep. [CodeWars](https://www.codewars.com/users/sign_in) is a good place to find challenges. Our challenge was to write a function that takes an array of Booleans. This function should return a Number that represents the number of times that true was present in the array. 

This was the example:

```var sheep = [
  true, true, true, false,
  true, true, true, true,
  true, false, true, false,
  true, false, false, true,
  true, true, true, true,
  false, false, true, true
];
countSheep(sheep);
// => 17```

## 2. Don't start typing! 
Psuedo-code and make a plan for how to solve your problem on paper or a whiteboard. Brainstorm a detailed yet readable description of what a computer program must do written in natural language rather than a programming language.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wiqzab3jui0jko86es28.JPG)

This whiteboard shows how my partner and I made sense of the problem initially and our ideas for our third possible solution (see step 5)!

## 3. Do any research on the tools you'll need to solve the problem.
My partner and I started by Googling 'check all elements in array javascript' and 'check each index in array javascript'. We didn't find any methods that would return a number. My partner thought to use a counter variable and a for loop to return the sheep count. We talked through the code and jotted it on our whiteboard.

## 4. Implement the solution in a programming language.
We only started programming after building a strong understanding of the problem and an idea for a solution. 
```
function countSheep(sheep) {
  var sheepCount = 0;
  for (var i = 0; i < sheep.length; i++) {
    if (sheep[i] === true) {
      sheepCount += 1
    }
  }
  return sheepCount
}
countSheep(sheep);

//return 17
```
We did it! But we wanted to strengthen our problem-solving muscles even more so we continued to step 5 of the process...

## 5. Challenge yourself further by trying to find another solution to the same problem.
After success with a for loop, we came back to trying to use an array prototype method. We perused the list of methods on [MDN] (https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype) and read more about any that seemed like they would work based on their name.
Our second solution used the .filter() method to filter true values from the sheep array into a new array then log the length of our filtered array.
```
function equalsTrue(value) {
  return value === true
}

var filtered = sheep.filter(equalsTrue)

console.log(filtered.length)
//17
```
We continued to challenge ourselves to find one more solution. This time we used .sort() and .splice() to sort the array alphabetically then remove the first values from the sorted array.
```
function sortedSheep() {
  sheep.sort();
  if(sheep[0] === false) {
  sheep.splice(0, 7)
  }
  return sheep.length
}
//return 17
```
Even though our additional solutions weren't the most intuitive, we learned more about programming through dialogue and practice!

##Why is this important?
Programming is problem-solving. If you want to get better at anything, you have to practice! Try problem-solving using these steps, comment about how it went and what you learned!