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

James Zwar
James Zwar
11,737 Points

Having problems with question 2. Tried various solutions like below

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

3 Answers

This worked for me:

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

Cheers!

Sean T. Unwin
Sean T. Unwin
28,690 Points

You're pretty close.

When dealing with arrays we use square brackets '[]`, whereas functions use rounded brackets '()'.

When you write saying2(a) this is like calling a function named saying and passing an argument of 'a'. That being said, you don't need to do this at all since the values a and b are items within the array that are looped through.

Also, for this function to perform you need to return something.

The body of the function should look like:

return a.length - b.length;