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

can't figure out question 2 regarding the saying2 sort by length.... any tips on this?

quiz / challenge questions here...

can't figure out question 2 regarding the saying2 sort by length.... any tips on this?

1 Answer

So the question is asking you to sort the Array by the shortest number of characters, to the greatest number of characters.

To do this you will need to add a compare function which the sort() function calls.

it will look something like this:

<!DOCTYPE html>

<script>
var saying2;

saying2.sort(function (a, b) {

        var compare = a.length - b.length;

        return compare;
</script>

What i did here was passed an anonymous function to the sort argument which then converts arguments a and b to numbers. Once compared it places the value back into the array. i suggest you look at the MDN site for a more better understanding of how it works.

I hope this helps.

If you have any more questions feel free to ask.