Report whether apple comes before banana.
Acceptance criteria
- 1.#out should show apple first.
JavaScript Tutorial · JavaScript Strings
These 10 tasks go with the JavaScript String Comparison 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: “Strings compare character by character using their character codes.” .
1.Compare alphabeticallyexercise · easy · 10 min
Report whether apple comes before banana.
Passes when: #out should show apple first.
2.Show the case ruleexercise · easy · 10 min · practice plan
Report whether a capital Z sorts before a lowercase a.
Passes when: #out should show capital first.
3.Sort with localeCompareexercise · easy · 10 min · practice plan
Sort the names the way a person would expect.
Passes when: #out should show apple,Mango,Zebra.
4.Compare fairlyexercise · easy · 10 min · practice plan
Trim and lowercase before comparing.
Passes when: #out should show they match.
5.Read what localeCompare returnsexercise · medium · 10 min · practice plan
Report the value when the two strings are identical.
Passes when: #out should show 0.
6.Fix the default sortbug fix · easy · 8 min
The default sort puts capitals before lowercase.
Passes when: #out should show apple,Mango,Zebra.
7.Fix the untrimmed comparisonbug fix · easy · 8 min · practice plan
The stray spaces make the two look different.
Passes when: #out should show they match.
8.Fix the comparison of lengthsbug fix · easy · 8 min · practice plan
Alphabetical order is wanted, not length.
Passes when: #out should show cherry first.
9.Fix localeCompare treated as a booleanbug fix · easy · 8 min · practice plan
Any non-zero result is truthy, so the wrong branch runs.
Passes when: #out should show b comes first.
10.Fix the sort that changes the originalbug fix · easy · 8 min · practice plan
sort works in place, so the original order is lost.
Passes when: #out should show Zebra,apple,Mango.
Between them these tasks cover Comparison, localeCompare and Sorting.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.