Monthly Archives: January 2016

Sordid sort()

Non-technical Explanation

An array is handy for storing information.  It’s kind of like a list that comes with a lot of special capabilities that makes it easy to manipulate the data in it.

Let’s say I have a ‘to do’ list:

var toDo = ['wash dishes', 'buy groceries', 'call mom']; 

Once you wash the dishes, you can call a method called shift that takes that first item off of your list.

todo.shift(); 

Now the updated array will look like this:

toDo = ['buy groceries', 'call mom']; 

Neat!  Right?  Yes…..but not when it comes to sort().

Story of sort()

As you code more and more, using these special ‘methods’ starts to become second nature.  However, sort() gets me almost every time because I always forget about what it actually does behind the scenes.  You can read all about it here, or read my thoughts below.

Usually when I want to sort things, I most often want to sort numbers, and usually in ascending order (from smallest to largest).   This works as expected with an array of positive numbers, as in this example:

var positive = [1, 4, 5, 2, 6, 3];

positive.sort();

[ 1, 2, 3, 4, 5, 6 ]

The resulting array has all the numbers from the original array but they are in order – which is exactly what I had intended to happen.  Now watch what happens when we try to do the same with negative numbers:

var negative = [-1, -4, -5, -2, -6, -3];

negative.sort();

[ -1, -2, -3, -4, -5, -6 ]

As you can see, they are ordered, but not in ascending order.  This is because Javascript converts the numbers to strings under the hood, and then puts them order based on each character’s Unicode point value.  This might be perfectly fine if we were sorting strings (i.e. – words), but in this case, this is NOT what I wanted to happen. What’s a programmer to do?

Compare Function

sort() went ahead and sorted the way it did because I didn’t give it specific enough instructions about what I wanted from the sort. Array.prototype.sort() can accept an optional compare function that you need to use to give it more specific sorting instructions.

If you want to sort numbers from smallest to largest, this is what you should use:


array.sort(function(a,b) {
  return a - b; 
});

Now, this compare function is telling Javascript to compare the values in the array as numbers. There is a little bit more to this about why it works, but for now just remember that if you don’t tell .sort() how you want something sorted, it will default to what amounts to alphabetical order!

To Future Self: Default Behavior

Dear Future Self,

Someday a project you work on might cause you to create a form and a submit button. If you are using jQuery to handle events, just realize that certain browsers have default behaviors that might happen.  For example, you might get an alert box popping up whenever you press a submit button.  Or, you might automatically go to a link when it is clicked on.  Whatever the circumstance may be, remember that you can prevent default behavior by using event.preventDefault().

Let’s pretend we are submitting a form, and we didn’t want an alert popping up.  You might get around that by writing something like this:


$('#messageSubmit').on('submit', function(event){
  event.preventDefault();
  etc...
});

Best regards,

Current Aimee

Re: contains

Non-technical Explanation

Imagine you are playing Go Fish.  Your opponent asks you for a certain card, you quickly sweep your eyes through your hand and realize that you either have it or don’t.  If you don’t have the card, you tell them “Go fish.”  In the case that your hand contains the card, you usually hand it over.

There are many times when programming were it is advantageous to write a contains function, or to use one in a popular library like underscore.js, to help you look through a collection or a data structure to see if you have a certain value or piece of information.  It returns true if it’s found, false if it’s not found.

Using Reduce

This was given to me as an example of how to use a function called reduce to solve contains:

_.contains = function(collection, target) {
      return _.reduce(collection, function (wasFound, item) {
        if (wasFound) {
          return true;
        }
       return item === target;
    }, false);

Reduce took me forever to wrap my head around.  The fact that is was being used to solve contains just about sent me into an existential crisis!

Reduce is basically like a powerful little machine that you can ask a question, send into a collection of data, and get an answer to your question.  It’s general enough so that you can set it up to do a variety of operations.  You could tally up the total sum of a list of numbers. You could have it keep track of certain types of data.  The possibilities are endless!  Reduce takes three pieces of information (parameters) in order to work: a collection, a function, and an accumulator.  The accumulator is basically the engine of reduce – it ‘accumulates’ its data as it travels through a collection of data.  Once it reaches the end of the collection, the accumulator is returned with the answer to your question.

In contains above, the accumulator is first set to the boolean value false.  It also is named ‘wasFound’ in the parameter list, and later in the ‘if’ block.  Remember, we are passing in a collection of data to contains, as well as the target value – the item that we want to check if it is there or not.  The line in reduce, ‘return item === target’, does the comparison of each item in the collection to the target value, and it returns a either true or false as it continues down the list.  If it returns true, that means that the item is indeed in the collection, which will trip the ‘if(wasFound) return true’ statement, breaking us out of reduce because we found the answer to our question – true, the item is in the collection!   If we reach the end of the collection without finding the target, false is returned to contains.

I had more to say about contains, but I might have to save it for another time!

 

Preparing for Hack Reactor Tips

After months of studying how to code by myself using mostly free online resources, I started to feel discouraged because I really couldn’t feel myself growing.  Being in NYC, I heard about a lot of coding bootcamps that were popping up, which piqued my interest.  I went to many open houses, and talked to everyone I knew who had done an immersive.  I also asked to speak to friends of friends who had done immersives.  Hack Reactor kept coming up in my research.

Why HR?

Generally, there are two types of bootcamps: those that take students from “0 to 80%,” and those that take you from “20% to 100%.”  Being that this is my second major career change, I wanted a program that I felt would prepare me to enter a new industry as best as it could.

HR Core 

Hack Reactor started out in San Francisco, where a good friend of mine attended.  I knew that moving to San Francisco for three months was completely off the table for me, and then my friend told me about Remote Beta – HR’s Remote program.

Remote Prep

HR started a month-long program called Remote Prep which I joined for the month of October in 2015.  They advertised the program as a way to bring you from 0 to the 20% you needed to pass the HR interview.  It ended up being a great way for me to test out the remote experience while increasing my skill set.  I learned more in that month than I had studying on my own!  It also was a great way to get my feet wet with paired programming.  Even though I need my own “think time” to think through coding problems, I really love collaborating with others while coding.  Everyone’s mind works differently, and it is great to see how people might approach a problem differently than you.

Interview and Post-Interview  

I scheduled my interview for Friday the 13th of November.  I received my acceptance email on Monday!  I really had no idea what to expect from there on out.  Basically, they send you a contract and you have a chance to ask them questions.   I found their email response system a little annoying as sometimes different people would answer and not really know what question I was asking.

Prework

Word of advice on the prework: Do not sign the student agreement and give your deposit until you are absolutely ready and in a position where you can start the prework!  The prework is no joke, and HR expects you to complete all the assignments within a certain timeline.  They want to see that you are capable of doing the work as this is a fast paced program.

After getting all of my questions answered from admissions, I was so excited to start the prework that I didn’t think about my personal schedule.  I ended up getting access to the assignments the day I left to spend a week with my family for Thanksgiving (which is serious business for us).  I also had a miscommunication with HR about a soft deadline extension vs. a hard deadline.  Long story short – I managed to get all my work done on time, but I wished that I didn’t have the stress of getting it done when I already had major plans.  Once you commit to HR, you need to make sure you are in a position to make it your priority, even before the official program starts.  That is one thing I wished I had known before starting.

Monday

I am in cohort HRR13 which starts this Monday, January 11!  I guess 13 is my lucky number.  I’m spending the week reviewing my prework, learning new topics, and preparing my workspace for when class starts!