Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Łukasz Czuliński
8,646 PointsNoob mind-block: functions within functions.
I've been going through the Javascript Foundations at a pretty good pace until I hit my first function within a function.
Jim uses the following workaround to make the sort() function work numerically;
var myArray = [10, 44, 32, 1000, 0, 44, 3, 4];
myArray.sort(function(a,b){
return a - b;
});
This is probably extremely trivial for most, but I just don't understand the mechanism.
How does the a - b associate with every number in the array?
Subtracting two numbers just leads to another number (which is what the string is already made up of), so how does doing this help sort the array?
3 Answers
Nick Fuller
9,027 PointsThe javascript array.sort() function by default will turn each item into a string and then sort them logically as strings unless a function is passed as the argument.
You can test this in the console
[10,44,32,1000,0,44,3,4].sort();
[0, 10, 1000, 3, 32, 4, 44, 44]
Now if you want to pass a function as an argument, the function must take 2 parameters. The standard community convention says to name them a and b, but you can name them whatever you want. The results of these two parameters should end up as one of three options.
- A value less than zero if the first parameter is less than the second
- A value greater than zero if the first parameter is greater than the second
- Or, zero if both the first and second parameters are equal
So when we call
var myArray = [10, 44, 32, 1000, 0, 44, 3, 4];
myArray.sort(function(first, second) {
return first - second;
});
the array will iterate over all its elements and try subtracting first from second. Depending on if the result is any positive number, any negative number, or 0 will dictate its place in the array.
So armed with this knowledge you can test it out in the console!
var myArray = [10, 44, 32, 1000, 0, 44, 3, 4];
myArray.sort(function(first, second) {
var result = first - second;
console.log("First is " + first + " and second is " + second + " so the result is " + result);
return result;
});
Now... try reversing the sort order! It's pretty simple, but a fun challenge!
Nick Fuller
9,027 PointsNo need to apologize, it's a legitimate question.
When it subtracts 10 - 44 = -34 it discovers two things; first, since it didn't equate to 0, they're not identical numbers. Second, it found out that the result of the equation is a negative number, therefore the first argument (10 in this case) will be sorted before 44 because it is smaller. Does that make sense?
Łukasz Czuliński
8,646 PointsYep, I get it. It just seems so illogical to me. 10 and 44 are already both numbers, so the fact you have to subtract 44 from to 10 to figure out which one is is smaller seems so strange. I get that sorting arrays doesn't go numerically to begin with, so I guess it's just one of those JavaScript quirks I need to get used to.
Thanks for the patience!
Łukasz Czuliński
8,646 PointsŁukasz Czuliński
8,646 PointsGreat post. Thanks! Your console.log at the end really helped to visualize what's happening in the function. I'm still just a bit unclear, however, of how the first - second subtraction dictates the place in the array for each number.
You say:
"the array will iterate over all its elements and try subtracting first from second. Depending on if the result is any positive number, any negative number, or 0 will dictate its place in the array."
So, for example, 10 - 44 is -34. How does this, combined with all the other subtractions (10 - 1000, etc), place 10 in the array?
I'm sorry if this is mind-numbingly simple, but something's just not letting me wrap my head around it!