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

JavaScript

Stuck on Challenge task 2

Once again I am stuck and really believing I am not smart enough for this! I've tried re-watching current video and video on length to no avail. I've also tried to find help on the web but only increased my confusion. Any help would be appreciated.

task: "Sort the 'saying2' array, on about line 19, so that the words are listed in length order. The shortest first. Use the 'length' property on the strings in the sort function."

code: var saying2 = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog's", "back"];

saying2;

4 Answers

Hi Sharon!

To sort the saying2 array items based on the string lengths, you will need to pass an anonymous function to the sort method. The function will take two parameters (which are two different elements from the array), and it will return a number to the sort method.

saying2.sort(function(a, b){
    //insert function definition here that returns a number
});

The sign of the number that the function returns will determine how the array is sorted. If the number is negative, it means that a should go before b in the array. If the number is positive, it means that a should go after b in the array. If the number is zero, then a and b can be put in either order, as they are considered equal.

Keeping that in mind, we need to figure out what goes inside the anonymous function. We want the strings to be sorted by their length, from shortest to longest, so we will need to use the length property.

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

Looking at the above code, we can see that if a and b are the same length, the function will return zero, and a and b will be put in either order. If the length of a is less than the length of b, it will return a negative number, and a will go before b, just as we wanted. If the length of a is greater than the length of b, the function will return a positive number, and a will go after b in the array, which is also what we wanted. Through this function, the sort method will be able to sort the strings from shortest to longest.

Hope that helped! Feel free to ask if you need further clarification.

Sharon Scheel I did think at some stage I could not do it either. But like anything in life, practice makes it better. I'm going to explain this not. sort when called need help to be executed and a return. after adding sort to the array you need to sort in this case : saying2 will become saying2.sort. then sort will use mathematics to solve whatever you want to do in this case it will be a substraction. but first you will have to give the function value that will be a and b. now we have: saying2.sort (a,b) . then open your bracke --- saying2.sort (function(a,b) { now you substract value a to b to get low values. using return: so ge have return a.length-b.length; }

but since you opened a bracket after saying.sort ( fucntion.....(){

return....... } ); closing the bracket at the end is important to execute tu funtion, if missed an error will appear.

to conclude your result is: saying2.sort (function (a,b) { return a.length-b.length; });

Thanks so much. I get so frustrated with this instructor. He uses the simplest possible of examples and then asks very difficult challenge questions - at least they seem very difficult to me. I end up trying so many different codes that I end up lost and confused - I can't wait to be done with this series!!

I think do something like this, I havent seen the video though.

saying2.sort(function(x, y){
  return y.length - x.length; // ASC -> a - b; DESC -> b - a
});