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:
- Pseudocode – great for getting an overview of the steps needed to process a task
- Trying something – great for getting started and when things do not work on the first try it can direct me to where I need to direct my research.
- Rubber ducky method – this is more of a method that I would use for reviewing written content.
- Reading error messages – often quite helpful although is rather limited to pointing out errors that are more of the syntax variety.
- Console.logging – when you have tried some code (found in the documentation or through Google) this is excellent for going through it step-by-step to see what it is doing and where the breakpoints are.
- Googling – once you know what to Google and are familiar with the context from learning/working with JavaScript this becomes almost cheating.
- Asking your peers for help – the short-circuit method to problem-solving. I have asked for a lot of help while learning programming in the past, which has aided my learning journey with JavaScript and Dev Academy a lot. This is not my first choice of problem-solving method as I have quite a bit of respect to the problem-solving process as being important to my learning journey.
- Asking coaches for help – similar to above this is a short-circuit method to problem-solving.
- Improving your process with reflection – thinking back this is probably what developed my current problem-solving process as I can see how helpful various techniques and tools were to figuring out a problem.