JavaScript Array sort — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Array sort lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Sort numbers ascending
easy10 minSort a list of numbers smallest first and join with commas.
- 2
Sort numbers descending
easy10 minSort a list of numbers largest first and join with commas.
- 3
Sort names
easy10 minSort a list of names alphabetically and join with commas.
- 4
Sort objects by a property
easy10 minSort users by age and show the ages joined by commas.
- 5
Sort without changing the original
medium10 minSort a copy and show the original order joined by commas.
Find the bug(5)
- 1
Fix the missing compare function
easy8 minThe default sort compares values as text, so 10 lands in the wrong place.
- 2
Fix the backwards compare
easy8 minThe list comes out largest first when it should be smallest first.
- 3
Fix the original being sorted
easy8 minsort changes the array, so the original order was lost.
- 4
Fix the object compare
easy8 minObjects cannot be subtracted; the property must be compared.
- 5
Fix reverse used to sort
easy8 minreverse only flips the order; it does not sort.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace