OK, rookie. It's time to train you on how to modify the shuttle's cargo manifest. The following actions will teach you how to add, remove, modify and rearrange our records for the items stored in our hold.
Create an array called
practiceFile with the following entry: 273.15.
push method to add the following elements to the array. Add
items a & b one at a time, then use a single
push to add the items in
part c. Print the array after each step to confirm the changes.
false, -4.6, "87"
Congratulations, rookie. You can now add items to an array.
unshift are used to add/remove elements
from the beginning/end of an array. Bracket notation can be used to
modify any element within an array. Starting with the
['oxygen tanks', 'space suits', 'parrot', 'instruction manual',
'meal packs', 'slinky', 'security blanket'], write statements to do the
'slinky'in the array with
'space tether'. Print the array to confirm the change.
pop. Print the element removed and the updated array.
shift. Print the element removed and the updated array.
unshiftrequire arguments inside the
(). Add the items 1138 and '20 meters' to the array - the number at the start and the string at the end. Print the updated array to confirm the changes.
Status check, rookie. Which array methods ADD items, and where are the new entries placed? Which methods REMOVE items, and where do the entries come from? Which methods require entries inside the ``()``?
splice method can be used to either add or remove items from an
array. It can also accomplish both tasks at the same time. Review the
splice appendix if you need a syntax reminder. Use
splice to make the following changes to the final
from exercise 2. Be sure to print the array after each step to confirm your
'keys'at index 3 without replacing any other entries.
indexOfis helpful to avoid manually counting an index).
Well done, cadet. Now let's look at some finer details about array methods. We've got to keep our paperwork straight, so you need to know when your actions change the original records.
push---alter the original array,
while others do not. Use the arrays
holdCabinet1 ['duct tape', 'gum', 3.14, false, 6.022e23]
holdCabinet2 ['orange drink', 'nerf toys', 'camera', 42, 'parsnip']
to explore the following methods:
sort. Refer back to the chapter if you need to review the proper syntax for any of these methods.
concaton the two arrays. Does
concatalter the original arrays? Verify this by printing
holdCabinet1after using the method.
sliceof two elements from each array. Does
slicealter the original arrays?
reversethe first array, and
sortthe second. What is the difference between these two methods? Do the methods alter the original arrays?
Good progress, cadet. Here are two more methods for you to examine.
split method converts a string into an array, while the
method does the opposite.
str = 'In space, no one can hear you code.', see what happens when you print
str.split(''). What is the purpose of the parameter inside the
arr = ['B', 'n', 'n', 5], see what happens when you print
arr.join(''). What is the purpose of the parameter inside the
joinchange the original string/array?
Nicely done, astronaut. Now it's time to bring you fully up to speed.
Arrays can hold different data types, even other arrays! A multi-dimensional array is one with entries that are themselves arrays.
element1 = ['hydrogen', 'H', 1.008]
element2 = ['helium', 'He', 4.003]
element26 = ['iron', 'Fe', 55.85]
table, and use
push(arrayName)to add each of the element arrays to it. Print
tableto see its structure.
table. Don't just nod your head! I want to HEAR you describe this difference. Go ahead, talk to your screen.
tablearray, print the mass of element1, the name for element 2 and the symbol for element26.
tableis an example of a 2-dimensional array. The first "level" contains the element arrays, and the second level holds the name/symbol/mass values. Experiment! Create a 3-dimensional array and print out one entry from each level in the array.
Excellent work, records keeper. Welcome aboard.