My approach to problem-solving

A time when I was blocked by a simple problem

Dev Academy is such a fast-paced course that I keep having to review documentation for simple concepts like functions and objects. I was blocked putting together the enterScores() function for the JS-Gradebook project.

At first I Googled to get an idea for how I could do this and ‘tried something’. This encouraged me to use the Array.forEach() method, but now I had to get it working. I still had an error message on my screen (‘undefined’ – a classic of the genre), but it was not directing me towards a way to solve my problem this time.

So I went back to basics and reviewed the documentation for Working with Objects on MDN. Then after having a read of that I closed my MacBook, put on my workout outfit, and went for a run. Thinking through how I can work with Objects while on the run, I thought of a solution to try when I got back to the keyboard:

function enterScores( gradebook, students, scores ) {
  // For each students[] element create in gradebook{} a student{}
  // Add scores[] to student{} using matching index

  students.forEach((name, index) => {
    gradebook[name] = {
      testScores: scores[index]
    }
  })
}

When I got back and tried out this new idea, it worked! The solution I thought of while “not working” on the task was exactly what I needed to solve the task.

Elegantly solving a problem

JavaScript methods are perfect for solving problems in an elegant and readable way. The first time I had a moment where I (humbly) admired the elegance of the code I had produced was when coding SuperFizzBuzz.

FizzBuzz is a familiar challenge to anyone who has done a programming course before – as it was for me. Reconstructing a decent fizzbuzz() function was rather straightforward. But then it was on to super_fizzbuzz().

Starting off I had to stop myself from recreating fizzbuzz() again, I have already created a fizzbuzz() function, I should use it. So now I have an array, that I want to iterate through, and for each element call a function and then return the array with the results of calling that function. Hmm, off to do some Googling.

Google presents a few solutions using Array methods: Array.every(), Array.forEach(), and Array.map(). Wait, Array.Map()! I just learned about that and what it does, that is perfect! So now all that was left was to work through how to use Array.Map() in an arrow function:

function super_fizzbuzz(arrayParameter) {
  return arrayParameter.map((element) => fizzbuzz(element))
}

One beautiful line of code

How I feel using each of these problem-solving techniques/processes:

Rubber Ducky Debugging Explained

  1. Acquire or create a rubber duck (or rubber duck substitute)
  2. Place rubber duck on desk and inform it that you are going to go over some code with it
  3. Explain to the duck what your code is supposed to do, go into detail and explain your code line by line
  4. At some point you will tell the duck what you are doing next and realise that is not what you are actually doing

Explaining a problem to someone else helps you to understand the problem better and find a solution to it. It encourages you to see the things from a new perspective and notice things that you may have been missing. This is similar to reading your writing out loud, which may help you spot typographical and grammatical errors.

You may have found yourself doing rubber duck debugging before without even realising it or knowing what this debugging method is. You may have been in a situation where you encountered a problem, decided or been encouraged to explain the problem to a friend, and figured out the solution to your problem before you have finished explaining the problem to your friend.

Rubber duck debugging removes the need to disturb someone else, you can substitute the person for a pet or any other inanimate object. Take your time and go line-by-line, focusing on the meaning, intent, and context for each line.

Rubber duck debugging is not a problem-solving method that is exclusive to programming. You can use this technique in solving problems in all other aspects of your life!