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

Stuck on task 2

Task 2 in this challenge asks to sort an array of strings by length, using the "length" property on the strings. I can't figure out how to make parameters for my function in the "sort" method using the "length" property. I'm not even sure I'm properly applying the "length" property to the strings in my array.

Thanks in advance for help.

2 Answers

Sjors Theuns
Sjors Theuns
6,091 Points

Hi Jerilyn,

I'm haven't seen the task, but sorting an array by string length in JS can be done as followed:

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

where arr is the name of your array.

Calvin Nix
Calvin Nix
43,828 Points

Hey Jerilyn,

Could you post the current code that you are currently using?

The code that Sjors posted (when implemented correctly) will allow you to complete this challenge. However, if we approach this issue by first looking at your code I feel that you will learn a lot more.

Thanks, Calvin