An earlier version of this article was posted here, on Medium.

Intro

For those of you who need an introduction, you can find one here.

When I decided earlier this year that I was going to blog about my life, I was oblivious to how I might approach it. With that blissful ignorance I began to flesh out a draft of my approach to the task and it resulted in the article above.

Since then, I’ve read more blogs on tech topics, taking notes along the way and decided to switch up the format. I’m going to try and make a weekly series of blog posts about my challenges learning to code and how I overcome them.

This is the first post in that series and I plan to outline a problem I encountered at the end of my second month learning. It’s regarding loops in both Python and JavaScript.

top down view of MacBook Pro screen with code

 

 

Photo by Nate Grant on Unsplash


The Problem

That’s from the Udacity CS101 course, lesson number 5 which I encountered at the start of June. If you’re like me, you think it sounds simple enough…in application to solving a problem, boy, was I wrong.

The lesson (for me) gave adequate but not great detail about the syntax/components of a for loop and no support for problem solving techniques. So when the course asked you to create a function with a loop to give the factorial of a number input(n), I was puzzled to say the least.

I was left to “poke and hope” at the code for the problem. I could fathom that we needed to increment the (n) value down by 1 and reinsert it back into the function I declared but no matter how I wrote it, this happened:

infinite loop result in Python

 

 

 

That’s called an infinite loop result, I would come to find out. It looks like the code does nothing.

I was at a loss. Worse yet, I was beginning to question if I was cut out for programming. The fabled impostor syndrome I read about had begun to set in.

I decided to blame the language and move on with other areas of my studies.

“Stupid Python.” I said as I decided to focus on Codecademy and JavaScript (where I was somehow having a much better time). I thought eventually I’ll encounter something that will give me an idea.


The Solution

Three weeks passed and…nothing.

I had progressed with Codecademy and was enjoying it, but admittedly it was mostly HTML/CSS. I always had that problem in the back of my mind, though.

Then, finally, I made it to the more involved JavaScript lessons. I was excited because I could see that loops were a part of the curriculum.

At this point I had invested many hours into Codecademy and I knew the structure of their lessons and exercises would be detailed enough that I might get somewhere.

I was half right.

After taking the loop lesson and passing their basic exercise, I tried to program a factorial function and lo and behold! …

I got stuck.🤦🏻‍♂️

Now, it’s important that I mention my pride in my ability to learn. I never struggled in school. In fact, often I excelled with ease. Therefore, I was stubbornly insistent on not “cheating” and looking up the answer. I would learn this was misguided.

Besides, I didn’t really know who to ask for help. Or, how to word the question.

I had code in Python and JavaScript that would at least return a result. It was just wrong.

python code
It was returning the input in Python…🤬

JavaScript code
At least in JavaScript it would iterate!🤷🏻‍♂️

I thought hard about what to do and I caved. I watched the Udacity tutorial for Python and revisited the loops lesson on Codecademy. In both cases, the solution felt so obvious once I found it; it did not, however take away from the pride of solving it myself in JavaScript.

Here is what they should look like solved proper:

python code
The Python tells us that 5! = 120, hooray!🤘🏻🤓🤘🏻

JavaScript code
The JavaScript agrees! So, what’s the difference? Did you spot it?🤔

For Python, I remembered after seeing the solution video, that indentation and white space are critical to the syntax of your code and how it executes. So, with return result right under the while loop’s procedural code block it was returning the input. By simply aligning the return result with the while statement itself we get the proper result.

In JavaScript, the buggy results where a matter of where the console.log(result) was placed. When it is before the second to last closing curly bracket (}) then the loops code block includes it as an iterative step in the procedure. Outside of that second to last curly bracket and it returns just the final result!

If I hadn’t had the confidence of solving it in JavaScript after seeing it function in Python, I wouldn’t have had the bravery to ask for help on another problem later on that same day. Here is the start to the thread:

The response was fast and friendly. I thought, “No trolls? Is this really the internet?”

But user [at]domhabersack was helpful in making a suggestion without exactly giving the answer away.

I applied that suggestion and the code worked! I was even able to refine it from my initial solution to something that I thought was more readable/logical.

The imposter syndrome had subsided and I found my “groove” again.


Summary

I’m not sure if there will always be some wholesome meta lesson to wrap up my posts but I think today there is:

  1. It’s okay to not know the answer.
  2. It’s expected that you’ll need help when you’re starting out. So just ask!

Maybe I got lucky, but I’m holding out that this wasn’t a one-off. Faith in humanity restored.


Thanks for reading! If you liked this article…

Feel free to comment, share any open source projects or other suggestions at the bottom or just drop a line to share your thoughts!

If you want to reach me via e-mail and separate from this article, I’m open for freelance opportunities or just a friendly chat on your tech interests, click the button 😎

0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x

Pin It on Pinterest

Share This