-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_practice.rb
102 lines (76 loc) · 2.74 KB
/
array_practice.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# elements = []
#
# (0..5).each do |num|
# puts "adding #{num} to the list"
# elements.push(num)
# end
#
# p elements
# In the exercises below, write your own code where indicated
# to achieve the desired result.
# Two examples are already completed. Your task is to complete
# any remaining prompt.
#-------------------
# PART 1: Animals: Array Syntax
#-------------------
# EXAMPLE: write code below that will print an array of animals.
# Store the array in a variable.
# animals = ["Zebra", "Giraffe", "Elephant"];
# p animals
#
# # EXAMPLE: Write code below that will print "Zebra" from the animals array
# # YOUR CODE HERE
# p animals[0]
#
# # YOU DO: Write code below that will print the number of elements in array of
# # animals from above.
# puts animals.count
#
#
# # YOU DO: Write code that will reassign the last item in the animals
# # array to "Gorilla"
# animals[2] = "Gorilla"
# p animals
#
#
# # YOU DO: Write code that will add a new animal (type of your choice) to position 3.
# animals << "Cat"
# p animals
# YOU DO: Write code that will print the String "Elephant" in the animals array
#-------------------
# PART 2: Foods: Array Methods
#-------------------
# YOU DO: Declare a variable that will store an an array of at least 4 foods (strings)
foods = ["pizza", "cheese", "burgers", "cookies"]
# YOU DO: Write code below that will print the number of elements in the array of
# foods from above.
p foods.count
# YOU DO: Write code below that uses a method to add "broccoli" to the foods array and
# print the changed array to verify "broccoli" has been added
def add_broccoli(foods)
foods.push("broccoli")
p foods
end
add_broccoli(foods)
# YOU DO: Write code below that removes the last item of food from the foods array and
# print the changed array to verify that item has been removed
def remove_last(items)
items.pop
p items
end
remove_last(foods)
# YOU DO: Write code to add 3 new foods to the array.
# There are several ways to do this - choose whichever you'd like!
# Then, print the changed array to verify the new items have been added
# YOU DO: Remove the food that is in index position 0.
foods.shift
p foods
#-------------------
# PART 3: Where are Arrays used?
#-------------------
# Sometimes we need to hold on to multiple pieces of data, but have it grouped together in a list.
# This is why programming languages have arrays!
# One example of a web/mobile application that uses arrays is Instagram. Each user has a set of posts
# associated with their account. Each post, is one of potentially many, that are grouped together in a list, or, array.
# The post itself likely has more complex data, but here is one way we can think about it:
posts = ["image at beach", "holiday party", "adorable puppy", "video of cute baby"];