Copy the middle two values out of a list and join them with commas.
Acceptance criteria
- 1.#out should show b,c.
JavaScript Tutorial · JavaScript Arrays
These 10 tasks go with the JavaScript Array slice and splice chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript slice copies part of an array, while splice changes the array in place.” .
1.Take a sliceexercise · easy · 10 min
Copy the middle two values out of a list and join them with commas.
Passes when: #out should show b,c.
2.Slice from the endexercise · easy · 10 min · practice plan
Copy the last two values using a negative index.
Passes when: #out should show d,e.
3.Copy a whole arrayexercise · easy · 10 min · practice plan
Copy an array with slice, push onto the copy, and show the original length.
Passes when: #out should show still 3.
4.Remove with spliceexercise · easy · 10 min · practice plan
Remove two values from the middle and show what is left.
Passes when: #out should show a,d.
5.Insert with spliceexercise · medium · 10 min · practice plan
Insert two values in the middle without removing anything.
Passes when: #out should show a,b,c,d.
6.Fix slice given a countbug fix · easy · 8 min
The second argument is an end index, not how many to take.
Passes when: #out should show b,c.
7.Fix splice used to copybug fix · easy · 8 min · practice plan
splice changes the original, so the list was damaged.
Passes when: #out should show kept 4.
8.Fix splice removing when insertingbug fix · easy · 8 min · practice plan
A count of 1 removes a value that should have stayed.
Passes when: #out should show a,b,c.
9.Fix the copy that is not a copybug fix · easy · 8 min · practice plan
Assigning the array shares it rather than copying it.
Passes when: #out should show original has 2.
10.Fix splice starting in the wrong placebug fix · easy · 8 min · practice plan
The removal begins one position too early.
Passes when: #out should show a,b,e.
Between them these tasks cover slice, splice and Copying arrays.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.