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!
- Create an array and assign it to a variable.
- Log the first element of your array.
- Log the second element of your array.
- 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
- Log the second to last element of your array.
- Log the middle element of the array (rounded down if there are an even number of elements).