1 and 0 are arguments that you’re passing to the .splice() method. You are passing 1 to the start parameter and 0 to the deleteCount parameter. 1, in this case, is the index at which to start changing the array. 0, in this case, is the number of elements in the array we want to remove, starting at start (in this case, index 1).
However, this would be incredibly inefficient. Imagine if our list had more than just a few elements. What if it had dozens? Or hundreds? .append() allows us to add an element on to the end of a list and saves us the effort needed to rewrite the list each time we want to modify it.
Editing requires access to the source code. Changes made that way are referred to as maintenance done on the physical source file(s), even if just to add new default data points. This is impossible during runtime. We need our program to dynamically modify the data given new user inputs, state changes, etc. That’s why we have list methods inherited from the parent class that give us the ability to mutate values, add data to and remove it from the list. The data is dynamic and changes are made on the fly, by our program, not us.
Methods you will become familiar with are, .append(), .extend(), .insert(), .remove(), .pop(), and .sort(), among others. They are runtime methods, not maintenance tools.
Given the above information, is it possible for the argument to be something like
myList.splice(1,‘mango’) // thereby simply inserting Mango in that position? or is a second number necessary??
If a second number is necessary then that would mean that you are simply telling telling the line of command where to put the current “fruit” aka between the current index of 1 and 0, thereby becoming 1.
If that means free of assumptions, then yes. However, it is always a good idea to check with the documentation.
The first positional argument is the insertion index, meaning the value at that index will be moved to the right to free up the space.
The second positional argument is the deletion count (how many items to delete starting at the insertion index). Check what the default is, if there is one. Does the second argument default to zero when it is omitted?
The third positional argument is the item to insert. How many items can there be? How are they to be given?
On the understanding that we can insert multiple, comma separated items, that would mean the item at index 1, would be moved far enough to the right to permit complete insertion of the items, assuming there are no deletions.
Be sure to read up on this in the docs. Scroll up to the reply from 3 Feb '21 for a link to the MDN site.
If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start ), then all the elements from start to the end of the array will be deleted.
The way I read this, everything from index 1 and beyond will be deleted, and replaced by the four items given. This means we should specify 0, for no deletions, or some positive number for the number deletions.
In a workspace, try experimenting to see what happens, and document your findings with examples and outcomes.
You said that if the number 0 was 1 instead, the order will start with “banana” instead of apple… But, following this, if you insert something, in this case, mango, at “0” index, wouldn’t the list start with “mango”, “apple”; "banana; etc… ??? Assuming the second argument is 0 as well (not deleting)?
Thanks a million for understanding what made us so quizzical about altering lists with splice commands and, further, how to go ahead and show what removing with the second argument can look like, in more than one instance (with more than one objective vis a vis placement, that is). This is great.