I think your code works but you are confused about working of
I’ll try to explain you how splice works,
splice() takes arguments as follows:
index from where you want to start removing elemmets,
number of element(s) you want to remove,
here you can add any number of items you want and they will be added to array in place of removed items
For example if i do:
var arr = [1,2,3,4,5];
Here is how it goes,
- We will start removing items from index 1 that is from second element onwards.
- We will remove 2 items from the array
- At this stage
arr will be
- But, we have also entered items we wish to replace the removed items with. So, those items will be added.
Can you try to tell me, how will
arr look like now?
Hope this helps.