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
Take a slice
easy10 minCopy the middle two values out of a list and join them with commas.
- 2
Slice from the end
easy10 minCopy the last two values using a negative index.
- 3
Copy a whole array
easy10 minCopy an array with slice, push onto the copy, and show the original length.
- 4
Remove with splice
easy10 minRemove two values from the middle and show what is left.
- 5
Insert with splice
medium10 minInsert two values in the middle without removing anything.
Find the bug(5)
- 1
Fix slice given a count
easy8 minThe second argument is an end index, not how many to take.
- 2
Fix splice used to copy
easy8 minsplice changes the original, so the list was damaged.
- 3
Fix splice removing when inserting
easy8 minA count of 1 removes a value that should have stayed.
- 4
Fix the copy that is not a copy
easy8 minAssigning the array shares it rather than copying it.
- 5
Fix splice starting in the wrong place
easy8 minThe 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