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 trialkathrynpeterson
16,001 PointsWhat is this code challenge question actually asking for?
The solution I supplied, hopefully included below, provides what I understand the question to be asking for: a sort of the array "saying2" which lists the elements of the array in order of length of the elements from shortest to longest. (I tested it in codepen and it works there.) In that context, the error message I'm getting, about the order of the array being wrong, makes no sense to me at all. So, clearly, I'm not understanding what the challenge is actually asking for. So, what is actually wanted here? Please clarify. Thank you!
<!DOCTYPE html>
<html lang="en">
<head>
<title> JavaScript Foundations: Arrays</title>
<style>
html {
background: #FAFAFA;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>JavaScript Foundations</h1>
<h2>Arrays: Methods Part 1</h2>
<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,z) {return z.length < a.length;});
</script>
</body>
</html>
2 Answers
Dustin Matlock
33,856 PointsHi Kathryn, here's a detailed explanation that might help.
Dylan Macnab
24,861 PointsIt's been a little while since I took this course but I played with this one a little bit and was able to come up with a passing solution.
You used the < value which returns a boolean value. The - returns an integer value that the .sort() function uses to sort the array by length. Hope that helps.
<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) {
return a.length - b.length;
});
</script>
kathrynpeterson
16,001 PointsThank you both very much! I did do a quick search for a prior answer, but apparently didn't go far enough back to find it. I wasn't feeling particularly patient last night when I was first wrestling with this. I'm sure I encountered this before in my community college class on the topic, too, but this and the PHP/MySQL stuff was where I had the most trouble, so it's not surprising that I did, and probably will more, again. Sigh.