DEV Community


Posted on • Updated on

Be a Problem Solver Challenge!

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 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 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
// => 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

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

//return 17
Enter fullscreen mode Exit fullscreen mode

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 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)

Enter fullscreen mode Exit fullscreen mode

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() {
  if(sheep[0] === false) {
  sheep.splice(0, 7)
  return sheep.length
//return 17
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)