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 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

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?