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
Amin Ahmed khan
5,371 PointsChallenge - Array Methods - Help Please
Question Sort the 'saying2' array, on about line 19, so that the words are listed in length order. The shortest first. Use the 'length' property on the strings in the sort function.
<script>
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();
// This is what I did , But it gives an error
saying2.sort(function(a,b) {
return b.length - a.length;
})
</script>
Please Tell me what i am doing wrong Code challenge
6 Answers
Amin Ahmed khan
5,371 Points[Solved] its -> return a.length - b.length;
scott duke
5,033 PointsI finally passed, a day later. I would have never thought to return the length of both a and b, but after seeing it, it totally makes sense to me now. I love how Jim makes us think... Or me, at least. :-)
Natacha S
11,561 Pointswhat i can see at first is a spelling error in length...
Natacha S
11,561 Pointswhat i can see at first is a spelling error in length...
Natacha S
11,561 Pointswhat i can see at first is a spelling error in length...
Amin Ahmed khan
5,371 Pointsignore the spelling error its still not working after correct spelling
Greg Shilling
3,723 PointsGreg Shilling
3,723 PointsYep. When the function returns a negative number, 'a' in this example (or arguments[0]) is sorted first, if it returns a positive number, 'b' (or arguments[1]) is sorted first.