Skip to main content

JavaScript String trim — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript String trim 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

    Trim the ends

    easy10 min

    Remove the spaces around the name.

  2. 2

    Detect an empty field

    easy10 min

    Report that a field of spaces is really empty.

  3. 3

    Pad a number

    easy10 min

    Add a leading zero to the minutes.

  4. 4

    Build a clock time

    easy10 min

    Pad both parts to make a proper time.

  5. 5

    Mask a card number

    medium10 min

    Show only the last four digits.

Find the bug(5)

  1. 1

    Fix the untrimmed check

    easy8 min

    A field of spaces is being treated as filled in.

  2. 2

    Fix the trim result discarded

    easy8 min

    trim returns a new string and does not change the original.

  3. 3

    Fix padEnd used for a leading zero

    easy8 min

    The zero belongs at the front.

  4. 4

    Fix the number not converted

    easy8 min

    padStart is a string method, so the number needs converting first.

  5. 5

    Fix the target length too short

    easy8 min

    The mask needs to reach twelve characters.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace