Skip to main content

JavaScript String slice — exercises and bug fixes

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

    Take the first word

    easy10 min

    Take the first four characters of JavaScript.

  2. 2

    Take the rest

    easy10 min

    Take everything from position four onwards.

  3. 3

    Take the last three

    easy10 min

    Use a negative index to take the extension.

  4. 4

    Drop the extension

    easy10 min

    Take everything except the last four characters.

  5. 5

    Shorten long text

    medium10 min

    Cut the text to six characters and add an ellipsis.

Find the bug(5)

  1. 1

    Fix the end index included

    easy8 min

    The end index is not part of the result, so it takes one too few.

  2. 2

    Fix substring given a negative

    easy8 min

    substring treats a negative as zero, so it returns everything.

  3. 3

    Fix the wrong end trimmed

    easy8 min

    The extension should go, not the name.

  4. 4

    Fix the missing ellipsis

    easy8 min

    Shortened text should show that it was cut.

  5. 5

    Fix charAt given a negative

    easy8 min

    charAt does not accept negative indexes; at does.

Ready to write the code?

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

Open the workspace