- 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 arrays
.
- Run
learn
. - 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.
- Run
learn submit
when finished!
Define 4 const
called append
, prepend
, removeLast
and removeFirst
to
an initial value of ["Milo", "Otis", "Garfield"]
. These are the array
s that
we're working with for each exercise.
NOTE: "Append" means "add to the end" and "prepend" means "add to the beginning."
- Append the driver "Odie" to the end of
append
. - Prepend the driver "Odie" to the beginning of
prepend
. - Remove the last driver from
removeLast
. - Remove the first driver from
removeFirst
.
We put our array knowledge into practice by writing and manipulating arrays. We also covered the concept of mutating state.