Skip to main content

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. 1

    Compare alphabetically

    easy10 min

    Report whether apple comes before banana.

  2. 2

    Show the case rule

    easy10 min

    Report whether a capital Z sorts before a lowercase a.

  3. 3

    Sort with localeCompare

    easy10 min

    Sort the names the way a person would expect.

  4. 4

    Compare fairly

    easy10 min

    Trim and lowercase before comparing.

  5. 5

    Read what localeCompare returns

    medium10 min

    Report the value when the two strings are identical.

Find the bug(5)

  1. 1

    Fix the default sort

    easy8 min

    The default sort puts capitals before lowercase.

  2. 2

    Fix the untrimmed comparison

    easy8 min

    The stray spaces make the two look different.

  3. 3

    Fix the comparison of lengths

    easy8 min

    Alphabetical order is wanted, not length.

  4. 4

    Fix localeCompare treated as a boolean

    easy8 min

    Any non-zero result is truthy, so the wrong branch runs.

  5. 5

    Fix the sort that changes the original

    easy8 min

    sort 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