I’m lil bit confused about using splice() to add elements to an array:
It has been mentioned that splice() takes 3 parameters at most, and we know that parameters are separated by comma, how could I define 4 parameters to pass the challenge?
I tried to pass the 2 elements as an array but it doesn’t work …
Hey, @systemry420,
I think your code works but you are confused about working of splice().
I’ll try to explain you how splice works,
splice() takes arguments as follows:
splice(
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];
arr.aplice(1,2,9,"hello","world","hey");
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 arr.
At this stage arr will be [1,4,5]
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.