forked from turingschool/m0_be_conditionals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if_statements.rb
135 lines (108 loc) · 3.12 KB
/
if_statements.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# In the below exercises, write code that achieves
# the desired result. To check your work, run this
# file by entering the following command in your Terminal:
# `ruby if_statements.rb`
# Example: Using the weather variable below, write code that decides
# what you should take with you based on the following conditions:
# if it is sunny, print "sunscreen"
# if it is rainy, print "umbrella"
# if it is snowy, print "coat"
# if it is icy, print "yak traks"
weather = "sunny"
if weather == "sunny"
p "sunscreen"
elsif weather == "rainy"
p "umbrella"
elsif weather == "snowy"
p "coat"
elsif weather == "icy"
p "yak traks"
else
p "good to go!"
end
# Experiment with manipulating the value held in variable 'weather'
# to print something other than 'sunscreen'
weather = "rainy"
if weather == "sunny"
p "sunscreen"
elsif weather == "rainy"
p "umbrella"
elsif weather == "snowy"
p "coat"
elsif weather == "icy"
p "yak traks"
else
p "good to go!"
end
weather = "icy"
if weather == "sunny"
p "sunscreen"
elsif weather == "rainy"
p "umbrella"
elsif weather == "snowy"
p "coat"
elsif weather == "icy"
p "yak traks"
else
p "good to go!"
end
##################
# Using the num_quarters variable defined below, determine
# if you have enough money to buy a gumball. A gumball costs
# two quarters.
# Right now, the program will print
# out both "I have enough money for a gumball" and
# "I don't have enough money for a gumball". Write a
# conditional statement that prints only one or the other.
# Experiment with manipulating the value held within num_quarters
# to make sure both conditions can be achieved.
num_quarters = 0
if num_quarters >= 2
puts "I have enough money for a gumball"
else
puts "I don't have enough money for a gumball"
end
num_quarters = 4
if num_quarters >= 2
puts "I have enough money for a gumball"
else
puts "I don't have enough money for a gumball"
end
#####################
# Using the variables defined below, write code that will tell you
# if you have the ingredients to make a pizza. A pizza requires
# at least two cups of flour and sauce.
# You should be able to change the variables to achieve the following outputs:
# If cups_of_flour = 1 and has_sauce = true, print "I cannot make pizza"
# If cups_of_flour = 5 and has_sauce = false, print "I cannot make pizza"
# If cups_of_flour = 2 and has_sauce = true, print "I can make pizza"
# If cups_of_flour = 3 and has_sauce = true, print "I can make pizza"
# Experiment with manipulating the value held within both variables
# to make sure all above conditions output what you expect.
cups_of_flour = 1
has_sauce = true
if (cups_of_flour >= 2) && has_sauce == true
puts "I can make pizza"
else
puts "I cannot make pizza"
end
cups_of_flour = 5
has_sauce = false
if (cups_of_flour >= 2) && has_sauce == true
puts "I can make pizza"
else
puts "I cannot make pizza"
end
cups_of_flour = 2
has_sauce = true
if (cups_of_flour >= 2) && has_sauce == true
puts "I can make pizza"
else
puts "I cannot make pizza"
end
cups_of_flour = 3
if (cups_of_flour >= 2) && has_sauce == true
puts "I can make pizza"
else
puts "I cannot make pizza"
end