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

Trying to sort out my saying 2 based on length - here is the code - any thoughts on where im going wrong?

var saying1 = ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"];
      var saying2 = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog's", "back"];
      saying1.reverse();
      saying2.sort(function(length){
      return Math.length();
                    });
Calvin Nix
Calvin Nix
43,828 Points

Hey Naoimi,

I'm going through and looking at your code now.

Also you might have noticed that your code is now nicely formatted. I went ahead and did that for you. In the future please refer to the 'Markdown Cheatsheet', which can be found just under the text area when you are posting a comment, so that you too can make your code more readable.

thanks

2 Answers

Calvin Nix
Calvin Nix
43,828 Points

Hey Naoimi,

Inside of your sort function you will want to pass in 2 parameters.

Then inside of the body of your function you will want to return the length of the first parameter passed in minus the second parameter passed in. Try this in your code challenge. If you are still unable to get your code working you can find the code that I was able to get to work below.

--spoiler--

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

Thanks.