Udacity Array Splice
Udacity Array Splice
splice() is another handy method that allows you to add and remove elements from
anywhere within an array.
While push() and pop() limit you to adding and removing elements from the end of an
array, splice() lets you specify the index location to add new elements, as well as the
number of elements you'd like to delete (if any).
arg1 = Mandatory argument. Specifies the starting index position to add/remove items.
You can use a negative value to specify the position from the end of the array e.g., -1
specifies the last element.
arg2 = Optional argument. Specifies the count of elements to be removed. If set to 0, no
items will be removed.
item1, ....., itemX are the items to be added at index position arg1
Note - This video does not have an audio. It was created as a visual to aid learning.
splice() is an incredibly powerful method that allows you to manipulate your arrays in a
variety of ways. Any combination of adding or removing elements from an array can all be done
in one simple line of code.
Take a look at the MDN documentation(opens in a new tab) to see a long list of example code
snippets demonstrating the power of splice() and then try the next set of programming
quizzes.
Quiz Question
What does the donuts array look like after the following changes?
When you need to add and remove elements from an array at the same time, splice() is a
great option!
Directions:
James was creating an array with the colors of the rainbow, and he forgot some colors. The
standard rainbow colors are usually listed in this order:
Using only the splice() method, insert the missing colors into the array, and remove the color
"Blackberry" by following these steps:
1. Remove "Blackberry"
2. Add "Yellow" and "Green"
3. Add "Purple"
node rainbow.js
When your code runs, you should get this output:
Start Workspace
After starting your workspace, remember to keep the page open. Closing the page will shut down
the workspace and halt any associated processes.
Try the quiz on your own first. If you get stuck you can check the solution by clicking the button
below.
Output
In the Harry Potter(opens in a new tab) novels, children attending the Hogwarts School of
Witchcraft and Wizardry(opens in a new tab) belong to one of four houses: Gryffindor,
Hufflepuff, Ravenclaw, or Slytherin. Each year, the houses assemble a Quidditch(opens in a new
tab) team of seven players to compete for the coveted Quidditch Cup. Your task is to help the
coach determine if the team has enough players.
Directions:
Create a function called hasEnoughPlayers() that takes the team array as an argument
and returns true or false depending on if the array has at least seven players.
node quidditch.js
true
false
false
true
Start Workspace
After starting your workspace, remember to keep the page open. Closing the page will shut down
the workspace and halt any associated processes.
Try the quiz on your own first. If you get stuck you can check the solution by clicking the button
below.
const team1 = ["Oliver Wood", "Angelina Johnson", "Katie Bell", "Alicia Spinnet", "George
Weasley", "Fred Weasley", "Harry Potter"];
const team4 = ["Oliver Wood", "Angelina Johnson", "Katie Bell", "Alicia Spinnet", "George
Weasley", "Fred Weasley", "Harry Potter", "Hermione Granger", "Ron Weasley", "Neville
Longbottom"];
function hasEnoughPlayers(arrayInstance) {
if (arrayInstance.length >= 7) {
return true; // true is a boolean value. Do not return a
string "true"
} else {
return false; // false is also a boolean value. Do not
return a string "false"
}
}
console.log(hasEnoughPlayers(team1)); // should be true
Output
true
false
false
true
Directions:
In an earlier exercise, you created a crew array to represent Mal’s crew from the hit show
Firefly.
Later in the show, Mal takes on three new crew members named "Simon", "River", and
"Book". Use the push() method to add the three new crew members to the crew array.
const doctor = "Simon";
const sister = "River";
const shepherd = "Book";
node join.js
Note: your output may look different because of the way the terminal prints out the array.
Start Workspace
After starting your workspace, remember to keep the page open. Closing the page will shut down
the workspace and halt any associated processes.
Try the quiz on your own first. If you get stuck you can check the solution by clicking the button
below.
Output
Use the MDN Documentation(opens in a new tab) to determine which of these methods would
be best for reversing elements in this array:
You got it! Here's the code you could write to reverse the array above:
reverseMe.reverse();
sortMe.sort();
Did you notice that 10 appears before 2? Traditionally, the number 2 comes before 10. But in the
.sort() method, numbers are converted to Unicode strings, so 10 comes before 2 in Unicode
order.