Once again with the running out of time, this post may be a little shorter in length (keyword here is may). The coding portion took a really long time, though, so you can blame that for this. Today, upon my perusing of the JavaScript Project boards, I stumbled into the conditionals section. The first project was to write a function to compare two numbers. I thought… “That’s easy” and to no surprise, it was indeed easy.
… “But I thought you said the coding took forever??” … Let me finish what I’m saying!
By the way, I’ve started using JS Bin in which to code, so my visuals are going to look a little different. I think the important thing in learning JavaScript should be so much so about the HTML/CSS visuals and more about the results. So, with JS Bin I can just write JavaScript and hit run and it’ll populate right next to me. Couldn’t be easier.
The reason the coding took forever is, because the first challenge took the better part of a minute, I decided to apply it to a more difficult challenge. Take in an array of numbers and sort them smallest to largest. I did a problem like this years ago, so I thought it would be easy… but it definitely was not. Let’s jump in…
Compare Two Numbers
The first problem is pretty straight forward. Write you function which takes two parameters (num1, num2). Then write the logic: if num1 > num2, print num1, otherwise print num2. Call your function with two parameters like, compareNums(13, 22) and it spits out your result. So easy, I could barely write a whole paragraph for it.

Res-sort the Array
The problem seems simple enough and the logic isn’t that hard to grasp either. Loop through an array to find the smallest number and then push it into another array. Yes? There are some issues with this. First, if you keep looping through the same array, you’re going to keep getting the same number. Second, how do you get back to the first number? Lastly, I’m going to show you the method I ran into which makes all this drawn out logic basically irrelevant. Luckily, the more I explain this out loud in plain English, the shorter it becomes, so the explanation shouldn’t be so long.
First off, you’re going to have to create variables for the smallest number, the new array, and another for what will become the index of the smallest number. I’ll explain. Get rid the lowest number when you go through the array. You can use splice to do this pretty easily. Use your loop to find the smallest number. When you find the new smallest number, change the variable to this number and save the position on this number in the array (the index number) to you variable. Continue through the loop checking for the smallest number and if/when you finish, push that number into the new array and delete the number.
The issue with this is you’re not going to get all your numbers. I used the array [4, 9, -4, 1] and my new array only contained the one lowest number [-4]. So? Run another loop around that loop which starts at the second index to compare the numbers and run the splice/push methods. With this, we’re getting closer! Our array now contains 2 numbers [-4, 1]. This is because (probably a little obvious, but maybe not) we’re making the array length one index shorter with every pass of the loop. By the time we’ve sorted out the two smallest numbers, we’ve deleted them, and now our loop reached the end of the array on its third pass.
The only way I could figure out how to make this work was to create yet another for loop. This time around the whole thing. This first for loop will essentially keep us at the first index while delete the other two numbers. Then, it will move onto the second index, run the loop again which compares the first index to the second, and removes it and pushes it into the array, before finally doing the same with second index. Here’s my code, with my notes included, so apologies for that, unless they help you think through the problem.

Dot Sort
Pondering how terrible difficult this all turned out, I decided to turn to Google after I finished (always try to wait till you solve it) to see if I could find something easier and guess what? Turns out theirs a method you can use in JavaScript that does all the work for you. Dot sort() (or .sort() ) will take your array and sort is alphabetically or essentially, lowest to highest. Strap that baby on our array and it quickly and easily gives us our result.


