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”
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.index(“grumpy cat”)
1
What will the following array method return?
famous_wizards = [“Dumbledore”, “Gandalf”, “Merlin”]
famous_wizards.reverse
[“Merlin”, “Gandalf”, “Dumbledore”]
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.include?(“Garfield”)
False
How would you insert the item “nala cat” at the end of the array? (There are two ways)
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats «_space;“nala cat”
OR
famous_cats.push(“nala cat”)
How would you sort the following array?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.sort
How would you remove the last item of the array?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.pop
What will famous_cats look like after the code finishes running?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.pop
[“lil’ bub”, “grumpy cat”]
What will famous_cats look like after the code finishes running?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats «_space;“nala cat”
[“lil’ bub”, “grumpy cat”, “Maru”, “nala cat”]
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.size
3
What will the following array method return?
[“a”, “b”, “c”].join(“ “)
“a b c”
How would you retrieve the last item of an array?
famous_cats= [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.last
What will famous_cats look like after the code finishes running?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.shift
[“grumpy cat”, “Maru”]
How would you reverse the following array?
famous_wizards = [“Dumbledore”, “Gandalf”, “Merlin”]
famous_wizards.reverse
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.sort
[“grumpy cat”, “lil’ bub”, “Maru”]
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.first
“lil bub”
What will the following array method return?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.index(“Garfield”)
nil
What will famous_cats look like after the code finishes running?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.push(“nala cat”)
[“lil’ bub”, “grumpy cat”, “Maru”, “nala cat”]
What will famous_cats look like after the code finishes running?
famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.unshift(“nala cat”)
[“nala cat”, “lil’ bub”, “grumpy cat”, “Maru”]