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 trialJoshua Peters
1,739 Points"Use the 'length' property on the strings in the sort function" -having trouble following this
<script> var saying1 = ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]; var saying2 = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog's", "back"]; saying1.reverse(); saying2.sort(function(){ saying2.length; }); </script>
3 Answers
Jason Wiram
42,762 PointsYou are close. Your saying2.sort() function needs to have a couple of parameters passed in and then compared so it can "sort" them. You can choose generic variables such as 'a' and 'b' that will contain two elements of your array (starting with element 0 and element 1). You must then do an operation in the body of the function that will return a positive, negative or zero. In the following code it will take the length of element 'a' (3 for the word "the") and subtract the length of 'b' (5 for the word "quick"). This returns a negative number. That tells sort() that the element 0 is smaller and it leaves it there. If it returns a positive number it switches the elements (thus sorting them). It's a confusing one at first. Good luck and keep learning!
saying2 = saying2.sort(function(a,b) {return a.length-b.length});
J.T. Gralka
20,126 PointsBasically, the code challenge is asking you to sort the saying2 array from shortest word to longest word using the sort method.
Here's a hint: remember that the sort method for arrays can take a function with two arguments as its argument:
saying2.sort(function(a, b){
// Your code goes here...
});
Does that look familiar at all? Rewatch the lessons in this stage and reply back if you are still unsure of what to do.
Best wishes!
J.T. Gralka
20,126 PointsEdit
Sorry. My original post was formatted poorly because I forgot to close my code markdown with three tick marks. Hope the above message is more readable now! :)
Joshua Peters
1,739 PointsThanks Jason,
Your explanation was a huge help.
-Josh
Jason Wiram
42,762 PointsMy pleasure.