Activity: Arrays

Let's start with an array of some films, books, or tv shows that you like:

let films = ["Starship Troopers", "Total Recall", "Robocop", "Basic Instinct"]
let books = ["East of Eden", "White Noise", "100 Years of Solitude"]

And then let's use the .push() method to add more names!

//Let's add some values to our array using `.push()`
console.log("I have " + films.length + " favorite films")
//Push some more and see the length change!

What's broken in this code?

This code is trying to change the value of the first element to -10 and the value of the last element to 10, but it's broken! Why?!

myArray = 66,27,108,44,92,1,100
myArray[1] = -10
myArray[array.length] = 10

Pair with a new neighbor for these challenges!

  1. Create an array and assign it to a variable.
  2. Log the first element of your array.
  3. Log the second element of your array.
  4. Log the last element of your array. Hint: how can you find out how long your array is? It should always print out the last element even if you add or remove elements from the array
  5. Log the second to last element of your array.
  6. Log the middle element of the array (rounded down if there are an even number of elements).