- Practice writing arrays
- Practice manipulating arrays
We've seen how arrays can be created and manipulated. Now we can practice
putting it all together. To recap, push()
pushes elements onto the ends of
arrays, and pop()
pops them off; similarly, unshift()
adds elements to
the beginnings of arrays, and shift()
pulls them off. Now it's time to
practice what we've learned.
We're going to work with actions that mutate ("change") their underlying
structures (like pop()
, push()
, shift()
, and unshift()
).
Generally, it's good practice to avoid mutating a program's state whenever
possible. However, for now, we're going to focus on the fundamentals of working
with Array
s. Remember our workflow:
- Run
learn test
. - Read the errors; vocalize what they're asking you to do.
- Write code; repeat steps 1 and 2 often until a test passes.
- Repeat as needed for further tests.
learn test
will save your work and mark your assignment complete in Canvas once all the tests are passing.
First, we're going to create four arrays of pets. Define 4 const
called:
append
, prepend
, removeLast
and removeFirst
, and set all to an initial
value of ["Milo", "Otis", "Garfield"]
. These are the array
s that we'll work
with for each exercise.
NOTE: "Append" means "add to the end" and "prepend" means "add to the beginning."
- Append the pet "Odie" to the end of
append
. - Prepend the pet "Odie" to the beginning of
prepend
. - Remove the last pet from
removeLast
. - Remove the first pet from
removeFirst
.
We put our array knowledge into practice by writing and manipulating arrays. We also covered the concept of mutating state.