I haven’t written anything in here in a while because I’ve been busy looking for work and juggling the course work from three different sources. A recent experience was worthy of a write-up, though:

Every so often I encounter a challenge on freeCodeCamp that I find especially intimidating. I’ll sit there for quite some time beating my head against a wall trying to figure out why code I know should work doesn’t, only to find out later that I hadn’t fully read every single word of the instructions in the challenge and obeyed the rules set forth by them when crafting my code.

The Nesting For Loops challenge was no exception to this. What was different was the length of time I spent on it.

You’re given a function that accepts an array as a parameter, that provides you with a variable with a numeric value, and that returns the value of the variable. You’re also given a nesting array (an array that contains arrays as its elements or, as I like to think of them, an array of arrays) to test the function with.

Your mission is to write some nesting for loops inside that function that access every element in the array, multiplying the variable by each element and changing the value of the variable to the result each time.

It sounds so very simple. I figured I could bang it out in a minute. It took me two days…

I didn’t have time to work on it right then, so I copied the code into a text file so I could mess with it offline. When I had the time, I coded what I thought was the obvious answer and got an unexpected result that, quite to my surprise, was incorrect.

I questioned whether the logic I thought I’d used to come up with that code was actually logic after all. I concluded that it was. I tried the same code again (because who doesn’t?). I got the same unexpected and incorrect result (of course).

I tried rewriting that code in a different way (assigning the result to a variable and then acting on the variable) with no better luck. I came at it from several angles, changing my expressions and statements to try and see what was necessary to achieve what I felt was so simple. Each and every time I got unexpected behavior and an incorrect result.

I went into the freeCodeCamp JavaScript Help chat and let it be known up front that I didn’t want the answer (because the whole point of these challenges is to assure yourself that you’re capable of coming up with that on your own), but to find out whether I was misunderstanding what was expected of me in the challenge (although the instructions seemed quite clear).

A very helpful person linked me to a Gist he had created that explains that particular challenge without giving you the answer. It thoroughly reviews how nesting arrays work and provides you with a visual representation of them so that you get a good feel for what it is you’re doing when you access them or access their individual elements.

I studied that Gist and came away from it with a deeper understanding of the process of accessing nesting arrays, but was no closer to coming up with a solution for that challenge, because everything I had just reviewed/learned suggested that the first solution I had come up with was correct. Bafflingly, though, it simply didn’t work.

I started doubting whether I was really cut out for development at all since I obviously wasn’t capable of wrapping my brain around such a simple task and finding a way to accomplish it. Back to square one.

Instead of trying to solve the challenge with what I believed to be the correct code, I began creating what I knew to be garbage code just to see if I could get it to pass the test. That didn’t work either (much to my relief). The hope was that I could manage to pass the test in ways that didn’t seem logical to me and then find someone to explain why that was the correct solution afterwards. I needed to pass first, though, and that just wasn’t happening.

It was suggested to me, by someone I know, to do some external research on my own to get the answer, but I felt that if freeCodeCamp was handing me the challenge at that point, its teachings prior to that must be sufficient for me to already have the knowledge necessary to do this challenge without outside resources. It was a moral imperative for me to access only my own grey matter to do this task.

The following day I went at it again, creating the same code I’d already tried before, creating several variations of it, and creating some garbage code to try to get past this thing. I got nowhere. Everything I tried gave unexpected and incorrect results.

Finally, I decided to turn to Stack Exchange and see if I could find out how others access nesting array elements. To my surprise, the very first post I looked at contained a question from someone struggling with the same freeCodeCamp challenge that was causing me so much trouble. He, apparently, had the answer to it and wanted further understanding. His code was pasted just below. I could tell it was there out of the corner of my eye, but I hadn’t looked at it yet. I didn’t want to because that would be cheating. But I was oh, so tempted. Just one quick look! Then I’d know what the answer was and could reason it out and fully understand it once and for all…

So I looked. The code he had used to complete the challenge was the first code I had tried. I was stunned. Here I’d been beating my head against the wall for two days when the first thing I’d tried was, allegedly, the answer. This just didn’t make sense.

I copied his code and my code into a text file so I could compare them, and they were identical. I went to freeCodeCamp and copied the challenge code into my personal web sandbox and pasted his code into it and ran it and it worked. I then ran the code in my text file (with the same exact solution in it) and it failed. I deleted my code and pasted his (identical) code into it and ran it and it failed.

It finally occurred to me to compare the rest of the code (not the solution, but everything else that had been provided for me by freeCodeCamp), and I discovered an errant closing curly bracket in a place it most definitely didn’t belong beneath the one that had been provided for me originally. I hadn’t noticed it before because it was tucked within the portion of code I’m not supposed to edit, so I hadn’t even bothered analyzing that when I had gotten unexpected behavior. If I had, I would have realized that my nesting for loop wasn’t properly closed before the value of the variable was returned, which was exactly why none of the attempts were successful.

The moral of this story is that it still boiled down to my not having read and obeyed what I was reading when the challenge was set forth. If I hadn’t (accidentally or unintentionally) altered code I had no business altering (and wasn’t instructed to alter!), this really could have been banged out in less than a minute. The good news is that all of this self-inflicted torture will stick with me and I doubt I’ll forget it, so it will hopefully help in future challenges. Onward…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s