Array Methods Flashcards
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.include?(“Maru”)
True
How would you create the following array without hardcoding?
[1, 2, 3, 4, 5]
(1..5).to_a
How would I check to see if the array included “lil bub”?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.include?(“lil bub”)
Should return true
How would I check to see the number of elements in an array? (There are two ways)
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.size
OR
famous_cats.length
How would you create a new array called my_array using the class constructor?
my_array = Array.new
How would you grab the index of “Maru”?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.index(“Maru”)
How would you insert the item “nala cat” at the beginning of the array?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.unshift(“nala cat”)
How would you create a new array called my_array using the literal constructor?
my_array = [ ]
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.length
3
How would you retrieve the first item of an array? (There are two ways)
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.first
OR
famous_cats[0]
What will the following array method return?
(1..10).to_a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
How would you make the array [“a”, “b”, “c”] looking like the following?
“a.b.c”
[“a”, “b”, “c”].join(“.”)
What will the following array method return?
[“a”, “b”, “c”].join
“abc”
How would you retrieve the second item in the array famous_cats?
famous_cats = [“Cheshire Cat”, “Puss in Boots”, “Garfield”]
famous_cats[1]
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.last
“Maru”