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 trialNazaam Kutisha
7,667 PointsI need Help with this challenge
I could use some help to properly order this function using the length parmeter.
```<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();
saying2.sort(function.length());
</script>
1 Answer
notf0und
11,940 PointsTo sort an array by string length, you'll need to write a custom function. They seem to only want a function declared inside the ``` saying2.sort();
This coded passes the task:
```javascript
<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();
saying2.sort(function (a, b) {
if (a.length < b.length) {
return -1;
}
else if (a.length > b.length) {
return 1;
return 0;
}
});
</script>
Credit goes to this site for the code: http://www.elated.com/articles/sorting-javascript-arrays/
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsThere is also a shorter way to write that compare function.