Sunday, April 21, 2019

Day 014: Order & Syntax

I started on the Javascript part of the FCC exercises and was going through them pretty quickly due to a tiny bit of experience in js years ago and going over the Programming Foundations: Fundamentals with Simon Allardice.

That is, until I got to the Stand in Line exercise.

I am probably going to get some terminology wrong but I am explaining it in newbie/layman terms as I understand it.

So for this exercise, I knew I needed to push() and shift() - easy enough concepts. But I just could not get it right. It could have been the confusing-to-me instructions or the existence of the testArr. But after about 30 mins of banging my head over it, I am ashamed to admit that I looked at the hint which gave the solution. /bleh

Even so, I was still not understanding why the solution was why it was even after another 30 minutes of trying to dig up the reasoning. That feeling of maybe I'm not smart enough started to creep in so I decided to take a break. After lunch and a quick gaming session, I still wasn't feeling confident enough to go back to figuring it out. So I hopped on the indoor cycle for an hour and watched the first half of the JavaScript Essential Training with Morten Rand-Hendriksen (who is also great as well!). I realized I might be missing something and just needed to keep familiarizing myself with the rules.

I got to this part and just had to take a screenshot. Cue the eureka! light bulb.

Morten is my best friend now.
One of the things that tripped me up was that I was trying to fit the testArr variable into the standInLine function even though they didn't have much to do with each other at that point. I was even including numbers in my push() and shift() even though the code had not even assigned numbers yet!

The initial task was to create the instructions inside the functions first. I should have looked closer on the arr and item parameters from the function. Which is what should be used in the statements inside the function body. /doh!

function nextInLine(arr, item) 

The testArr was defined after the function and then later called upon in the display code that made use of the standInLine function. Remember, the function are instructions that can be used again and again by other variables. Even if you create a new variable of testArr2 and assign 11, 12, 13 to it, you can later use the same standInLine function to remove the first value and add in a new value.

After you get the solution, give the below a whirl after to see what I mean:

var testArr2 = [11,12,13]; 

console.log("Before: " + JSON.stringify(testArr2)); 
console.log(nextInLine(testArr2, 14)); // Modify this line to test 
console.log("After: " + JSON.stringify(testArr2));

I forgot that and tried to cram everything together. Judging from the forum, a whole lot more people than just me made the same mistake. The Python tutor tool for Javascript also really helped visualize the computing.

I do have a tendency to put the cart before the horse (like, I'm one of those people who does not mind spoilers and may even look them up myself), so I just have to remember that the order is really, really important in Javascript and every other language out there and I really need to stop trying to get ahead of myself.

Lastly, I included Syntax in the blog title because I am finding that most of my other mistakes are syntax related as opposed to logic mistakes. I am the queen of typos. I also often find myself missing a bracket or quotation here and there that breaks the code. I am not sure if there are ways to improve on this other than relying on the text editors or ide's to help me on preventing name typos and getting into the habit of opening and closing things before adding in the body... I am all ears if you have suggestions!

No comments:

Post a Comment