There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
The second position in the array is at index 1, which is the start of the splice sequence. The second argument is optional but it represents the number of elements to be deleted, in this case none.
could you explain your answer in another way? I think I still don’t understand… so the zero argument represents that no items are being deleted, correct?
so if we were making ‘mango’ the third item on the list the command would read:
myList = [‘apple’, ‘banana’, ‘pear’]
myList.splice(2, 0, ‘mango’) // make ‘mango’ the third item in the list
Given that only pear exists at the original index 2, and we are inserting at that index, to delete pear we give a deletion count of 1. Recall that the count has no connection to indices.
myList = [‘apple’, ‘banana’, ‘pear’]
myList.splice(1, 0, ‘mango’) // make ‘mango’ the second item in the list
// now, myList = [‘apple’, ‘mango’, ‘banana’, ‘pear’]
I’ve read the above but I still don’t get it.
Why can’t I put:
myList.splice(1, ‘mango’) to get mango at Index 1 position?
Did you try it? As I understand it if we leave off the second argument, JS inserts its own default value in its place, 0. Check to be sure this is in fact the case.
Deleting allows us to replace the value at a given index, or a group of values beginning at that index. If we don’t delete anything, the list shifts to the right to permit insertion of the new item at that index.
I still dont get it. When: myList = [‘apple’, ‘banana’, ‘pear’]
myList.splice(1, 0, ‘mango’)
Isn’t it supposed to be calling to index 1, which inside the array it is the position 1, so it should be apple, but as an index, index 1 it should recall to banana, which is the actual index 1, instead of apple.
Im kinda confused about this exercise tho.
Some help, or a way to be able to understand whi the splice goes with 1 instead of 0, which it would be the index value of apple inside the array by position?
Nvm i just understood the one means where we are putting the mango string inside the array, which is index 1, which is after the index 0 which is apple. And the optional value of 0 mens not removing anything inside the currently values inside the array.
Yep
Got it
Not a big deal.
Yes, we could, but it would be counterproductive. We are given a list, and must assume that the program will do the manipulation. We cannot access the list to make manual changes while the program is running. That is why list methods are available for the purpose of mutating the list.
Number 1 = It means the new position for the “mango” string keeping the normal order of the other items and moving “banana” one position forward. If the number was 0 there instead 1, the order will start with “banana” instead “apple”.
Number 0= It means that none item in the list is gonna be deleted.
Pretty much, yes. The first argument is the insertion point, so mango will take index position 1, and every element from there will be shifted to the right. The second argument is the deletion count, as in how many items will be replaced (if any) by the inserted slice.
if i write myList=[‘apple’,‘banana’,‘pear’]
mylist.splice(0,0,‘mango’)
the new list ll be mylist=[‘mango’,‘apple’,‘banana’,‘pear’]
is that right and what is the difference betweeen pop and splice