Sort a list of numbers smallest first and join with commas.
Acceptance criteria
- 1.#out should show 1,5,10.
JavaScript Tutorial · JavaScript Arrays
These 10 tasks go with the JavaScript Array sort chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript sort arranges the elements of an array and changes the original.” .
1.Sort numbers ascendingexercise · easy · 10 min
Sort a list of numbers smallest first and join with commas.
Passes when: #out should show 1,5,10.
2.Sort numbers descendingexercise · easy · 10 min · practice plan
Sort a list of numbers largest first and join with commas.
Passes when: #out should show 10,5,1.
3.Sort namesexercise · easy · 10 min · practice plan
Sort a list of names alphabetically and join with commas.
Passes when: #out should show Ada,Alan,Grace.
4.Sort objects by a propertyexercise · easy · 10 min · practice plan
Sort users by age and show the ages joined by commas.
Passes when: #out should show 22,30,45.
5.Sort without changing the originalexercise · medium · 10 min · practice plan
Sort a copy and show the original order joined by commas.
Passes when: #out should show 3,1,2.
6.Fix the missing compare functionbug fix · easy · 8 min
The default sort compares values as text, so 10 lands in the wrong place.
Passes when: #out should show 1,5,10.
7.Fix the backwards comparebug fix · easy · 8 min · practice plan
The list comes out largest first when it should be smallest first.
Passes when: #out should show 2,7,9.
8.Fix the original being sortedbug fix · easy · 8 min · practice plan
sort changes the array, so the original order was lost.
Passes when: #out should show 3,1,2.
9.Fix the object comparebug fix · easy · 8 min · practice plan
Objects cannot be subtracted; the property must be compared.
Passes when: #out should show 22,30,45.
10.Fix reverse used to sortbug fix · easy · 8 min · practice plan
reverse only flips the order; it does not sort.
Passes when: #out should show 1,2,3.
Between them these tasks cover sort, Compare functions and reverse.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.