Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Arrays Methods: Part 2

Tammy Herrera
Tammy Herrera
4,070 Points

Sort saying array on line 19, so the words are listed in length order. Shortest first. Use the 'Length' property on the

strings in the sort function.

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

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

I'm pretty lost with this question. Not sure how to solve it? Can someone please assist me?

1 Answer

11,940 Points

Your function needs to sort the array. Use this

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

You can switch a.length and b.length to return the array longest to shortest as well :)

Thomas Ireland
Thomas Ireland
Courses Plus Student 8,216 Points

This one had me stumped for ages too and I realised I was adding parentheses onto the .length property.

Thanks, Bryan H .

Michael Cleyn
Michael Cleyn
1,755 Points

Can you explain what a.length - b.length is doing?