Skip to main content

JavaScript Array slice and splice — exercises and bug fixes

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

    easy10 min

    Copy the middle two values out of a list and join them with commas.

  2. 2

    Slice from the end

    easy10 min

    Copy the last two values using a negative index.

  3. 3

    Copy a whole array

    easy10 min

    Copy an array with slice, push onto the copy, and show the original length.

  4. 4

    Remove with splice

    easy10 min

    Remove two values from the middle and show what is left.

  5. 5

    Insert with splice

    medium10 min

    Insert two values in the middle without removing anything.

Find the bug(5)

  1. 1

    Fix slice given a count

    easy8 min

    The second argument is an end index, not how many to take.

  2. 2

    Fix splice used to copy

    easy8 min

    splice changes the original, so the list was damaged.

  3. 3

    Fix splice removing when inserting

    easy8 min

    A count of 1 removes a value that should have stayed.

  4. 4

    Fix the copy that is not a copy

    easy8 min

    Assigning the array shares it rather than copying it.

  5. 5

    Fix splice starting in the wrong place

    easy8 min

    The removal begins one position too early.

Ready to write the code?

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

Open the workspace