JavaScript String Comparison — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript String Comparison 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
Compare alphabetically
easy10 minReport whether apple comes before banana.
- 2
Show the case rule
easy10 minReport whether a capital Z sorts before a lowercase a.
- 3
Sort with localeCompare
easy10 minSort the names the way a person would expect.
- 4
Compare fairly
easy10 minTrim and lowercase before comparing.
- 5
Read what localeCompare returns
medium10 minReport the value when the two strings are identical.
Find the bug(5)
- 1
Fix the default sort
easy8 minThe default sort puts capitals before lowercase.
- 2
Fix the untrimmed comparison
easy8 minThe stray spaces make the two look different.
- 3
Fix the comparison of lengths
easy8 minAlphabetical order is wanted, not length.
- 4
Fix localeCompare treated as a boolean
easy8 minAny non-zero result is truthy, so the wrong branch runs.
- 5
Fix the sort that changes the original
easy8 minsort works in place, so the original order is lost.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace