Inside My Head: Learning JavaScript

I have had a very productive weekend so far (and it’s only Saturday!). I am spending some of my day today going back through my easy Coderbyte solutions and cleaning most of them up. I will also be adding in comments for most of the lines explaining my logic. So a lot of the files will look like this:

 function FirstReverse(str) { 
   return str.split("")  //splits the string into an array of characters 
             .reverse()  //reverses the array 
             .join("");  //joins the array back into a string, with no space between the characters 

This is actually a lot of fun for me. Now that we’re getting into more abstract concepts like MVCs and servers it’s nice to just go back and power through some JavaScript code and understand exactly what it’s doing.

As always, you can find my coderbyte solutions on GitHub (I will be posting updates to it throughout the day), but I feel like I need to start adding a caveat, because of the amount of people who have said they found my blog looking for coderbyte solutions. Just staring at my code isn’t going to make you understand JavaScript. You need to do it for yourself. If you’re brand new to JavaScript, just seeing the cool tricks is not going to make you a programmer. I really recommend things like Udacity, Codecademy and other sites that talk about the fundamentals of comp sci/programming.

Time Complexity in Algorithms

Time complexity is a useful concept in programming. It’s essential for job interviews, but beyond that to really be a good programmer you need to know how cumbersome your algorithm is going to be. For 10 items, something that takes 2 seconds per item is only 20 seconds, but what if you have a million items?

In general, time complexity is measured by how long it takes a number of items (n) to be processed. It is the relationship between the number of things you have to do and the work to be done. Something that takes the same amount of time no matter have much of it you have to do, would be constant time. If I had 10 items or 1 million it would only take me 2 seconds to process. On another extreme is something called the handshake problem. If you only have two people in a room they only have to shake one another’s hands (one handshake). If you have 10 people, they each shake 9 other hands (55 handshakes total, we’ll get to the math in a moment). This is quadratic time.

Here’s a visual of the common programming time complexities:

Time complexities!


Lets start with constant time again. In programming, constant time is like deciding whether a number is even or odd:

 n % 2 = ? 

This is just one expression. You run and you’re done. It’s always going to take a certain amount of time, no matter what n you give it. In big O notation we would write this as O(1).

Next, my favorite, as far as understanding goes. Linear time. In programming, linear time, is like running through a loop of n numbers and doing something.

 for( var i = 0; i < n; i++ ){
   do something..

The more things (‘n’s) you have, the longer the algorithm is going to take. It’s a 1:1 ratio. If 1 thing takes 1 second, 2 things are going to take 2 seconds. This is O(n).

Quadratic time is worse than linear time. It’s like the handshake problem or in programming terms a for loop within a for loop. For each n we need to run through all the numbers 1 through n.

 for( var i = 0; i < n; i++ ){
   for( var j = 0; j < n; j++ ){
     do something..

Each n thing is done n times. This is n * n or in big O terms O(n2).

Logarithmic time is all kinds of magic. It tapers off to a near constant for larger ‘n’s. This is possible because of the magic of halves. In a binary search tree there are only two options to descend, left (options lower than the current value) or right (options higher than the current value). Either choice will cut off roughly half of all the possible values. On the next level, you have the same choice, either you’ve found your value, or you go left or right, halving your options again.

 while ( low <= high ) {
   var mid = ( low + high ) / 2;
   if ( target < list[mid] ){
     var high = mid - 1;    
   } else if ( target > list[mid] ){
     var low = mid + 1;
   } else { break; }

If you’re constantly halving the amount of things you have to check, eventually you hit a roughly constant time for an n search. This is O(log n).

This post has been brought to you by a forum post on time complexity, bits and pieces of the wiki article on time complexity, and this inspirational code class ad featuring my lead instructor from his Twitter days.

Week 3 – Less Work, More… Work?

School-wise, this week was very, very short. Today is Thanksgiving and I’m almost a little shocked that we got it off (although many of my peers are spending their day at the school if the emails about keys and door opening flying back and forth are a good reference). Because of the shortness I was thrown a bit off guard on Monday when I realized that it was time for our 3rd assessment already! This is week three! It feels simultaneously like I’ve been here for days and for years. The assessment went well and I actually remembered all my things from the previous week without too much panic. I did have a hilarious nightmare afterward that involved me being forced to code a merge sort algorithm using a pencil and a very limited amount of paper and my lead instructor yelling at me for my terrible handwriting (this is why I love computers! I have terrible penmanship).

Because of the short week we basically just went straight into Backbone.js┬áthis week, which for my non-coding followers is a JavaScript library that allows you to structure your app cleanly by dividing the work that must be done into the actual data “models” and the way you represent that data to individuals “views”. This concept is what’s called an MVC, which is one of those trendy/useful buzzwords you hear a lot in coding. Anyway, it’s what we did in class this week and I plan on working on it a bunch over my long weekend.

Unfortunately because this is the longest break I have besides solo project time during Christmas break I think I’ve put more on my to-do list than is physically possible (especially since I promised my platonic life partner Ava I would help her with her Hackbright project too). Lets run down what I have on my list:

  • Review algorithm time complexity (Big O notation)
  • Practice recursive problems
  • Make business cards:
    Personal business cards of awesome
  • Work on my Backbone project (we are working on it through next Tuesday, but I want to tackle some of the extra credit)
  • Redo/refactor some of my Coderbytes code – I’ve learned a bunch, I can probably do better
  • Research getting involved in some open source stuff/get some pull requests in to bigger projects
  • Maybe try learning Ruby on Rails (we might lose out on the Ruby on Rails sprint because of the timeline of holidays)

So yeah, I’m probably a crazy person. Today I will eat and hang out with friends and be merry though. Tonight I will allow the code to creep its way to the front of my brain again. I also plan on writing a more technical article on time complexity sometime this weekend if I can wrap my brain more fully around it so I can pass along the tips I find.

Week 2 – Programmers Do It Algorithmically

I have less pretty pictures for you this week. The most I saw of the sunshine looked similar to this:

View from the 8th floor

At least it was sunshine. It “rained” for like a minute one day and the umbrellas came out en masse. I was perfectly happy in a hoodie, but I definitely felt like an outsider. I might need to buy an umbrella, this whole trying to not look like a tourist thing is harder than I thought. Although I’ve had more people ask me for directions this week than I ever have in my life combined so I think the hair and the nose piercing are good SF camouflage.

This week was a teensy bit rollercoastery for me. We started out with a fairly easy CSS/fun jQuery tricks problem and then tackled a pretty epic sprint on the N-Queens problem. N-queens is the idea that you need to place n (a number) of chess queens on an nxn chess board so that none of the queens can attack any other queen. I think they’ve only solved it up to 27 ( and that was people from Hack Reactor). I got a little flustered with that one as algorithms were never my strong suit before this (it’s why I got the “conditionally acceptance” at App Academy – thank god, might I add). I think though I’m just being too hard on myself. Not every software engineer deals with something as crazy as the n-queens problem on an everyday basis. Our more traditional daily toy problems (similar to tech interview questions) are fairly straight forward and I can code a bubble sort algorithm in about 5 minutes (maybe 10 with one hand tied behind my back).

I think that’s what Hack Reactor has done for me more than anything else. It’s made me accept my flaws, but know that everyone has them and there is always more to learn. I’m a good intuitive coder, but I’m no good with the lingo of it. We all are here to learn to become great coders Software Engineers.

Short post this week, sorry. I have dinner with friends to go to and my Sunday is too peaceful and sunny (and full of awesome 50th anniversary Doctor Who) for any more words.

Week 1 – “Hell Week”

This article could easily be subtitled: Glimmering punctua of pure clarity and childlike wonderment. But then I’d be jacking my title from Marcus (our lead instructor/one of Hack Reactor’s co-founders). Hell week (as the instructors affectionately call week one) is semi-officially over and I am more content than I have ever been in my entire life. I will admit that as I got ready and rode the train into the city on Monday morning I had my doubts. Was I cut out for this, would anyone like me, was this program going to be totally not what I expected? Let me tell you my friends, it is more (any adjective is useless here) than I can describe.

On Monday morning I told myself not to be nervous, but the impostor syndrome and general unknown was super strong. The train ride was scary, the solo walk to the building was scary, the first few uncomfortable introductions in the kitchen area where breakfast is served were scary, but I have come to love this sight:

944 Market St

It’s so typical San Francisco. Teeny entryway wedged between a Payless Shoes and a sketchy looking pawn shop. It’s after you step into the elevator, which is typically a little creaky, and emerge on the top floor that the magic starts:

First Impressions

It's JavaScript all the way down

This week has been a blur. We had lectures that ranged from how to be an effective student to the principles of time complexity on different data structures. We implemented our own auto resizing hash tables (well some of us did, me and my excellent partner, Sara, who started out as a blog stalker apparently and ended up being an awesome lady and I’m glad to call her my friend, got through most of the extra credit work and so got to a point where we had to tackle resizing hash tables). We tried valiantly to learn 30ish names while trying to cram in as much computer science as possible (I can proudly say I know the whole junior class – my cohort). We learned the least and most reasonable places to eat and wander during the day and after dark and we all developed what I think will be a pretty awesome camaraderie that will definitely benefit me for years to come.

My life has been completely altered in ways I don’t even know how to quantify yet. We’ve covered computer science concepts in the past week that I learned over the course of a year back in college when I first tried to be a computer science major. The thought scared me at first. When I realized we were covering things I had learned before, I was afraid I would feel the same way about them, like I wasn’t good enough to get through them. That was always my fear in college, that I was just fooling myself and everyone around me.

So I spent at least half of this first week waiting for the other shoe to drop. Technically it did drop, but not in the ways I thought it would. It dropped when I realized that this thing that I can do with my brain and this keyboard is pretty damn amazing. That I am pretty amazing. I can have an off day, my recursion algorithm can go unexpected places and my crafted tests could not pass, but that’s the life of a software engineer. Things don’t just get done by typing out code as fast as your fingers can move, they get done by running into walls, refactoring, researching what other amazing people have done, and bouncing ideas off of your fellow engineers.

To sum up all that wall of text: Life is pretty damn awesome now and there was nothing hellish about this week.

Week 0 Wrapup

I meant to post before today, but the picture I wanted to use in this post wasn’t available yet and I managed to get a cold… Everyone at Hackbright was getting sick and Ava’s husband also got a cold, so I guess my time was nigh. Thankfully Ava’s an amazing nurse (she’s strict though, she won’t even let me have a Diet Coke).

I did have a fun week rest of the week in spite of how I feel today. Thursday we stayed at home and Ava got some awesome work done on her final project and I just played around some more with Angular tutorials and double checked her semi-colons in JavaScript. Thursday night though, was awesome. We did this:

Geek Girls

I got to go to my first ever geek girl dinner! Ava hooked me up and I really hope I can go to more in the future. We went to San Jose to visit CISCO‘s main campus. It was super inspiring and totally pumped me up for next week. Also they had tasty food and gave us T-shirts and I got yelled at by Ava for saying I was a student and not an engineer. I won’t make that mistake again!

Yesterday I trekked through three different transit systems to get back down to San Jose by myself to visit my other bestie and have dinner with her family. We had lots of fun and visited the giant library next to the SJSU campus. She and I share a ridiculous love of reading and I think she just wanted to rub it in that I will have no time to read for a long while.

Hack Reactor starts on Monday and I’ve been drinking tea and sitting around in my pajamas all day in hopes of feeling decent by Monday. Wish me luck!

Week 0, Day 3

Still pretty quiet here. I’ve been helping Ava with her game and looking at some basic tutorials for Backbone and Angular. Then I got sidetracked into learning VIM.

I did hike up the epicness that is Taylor St on a lunch break with Ava to go look at Huntington Park. The views were amazing and I love that I can still wear a T-shirt (no sweater/coat necessary) at all times, even on my evening trek to the BART station and “home” home (Ava was upset by the quotes around the word home so I had to change it).

On the way up:

Up Taylor St

Looking down:

Looking Down Powell St

Looking at the Bay Bridge through the buildings (another thing I love, it looks like stereotypical San Francisco everywhere, tall buildings, funky old fixtures, and a strange mixture of people types):

<img src=”,h_800/v1391709300/2013-11-06-12_47_39-768x1024_p9tbab.jpg” alt=”Bay Bridge on California St width=”600″ height=”800″>