-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise2.rb
102 lines (75 loc) · 1.66 KB
/
exercise2.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
# to_s
# to_i
# to_f
# to_sym
# :name_of_symbol
# if
# else
# elsif
# unless
# while
# until
# do this n times
# 1. How would you calculate a good tip for a 55 dollar meal? Use puts to print the answer onscreen.
tip = 55 * 1.15
puts tip
# 2. Try adding a string and an integer. What happens?
rand_int = 48694796531
puts "BitMaker " + rand_int.to_s
# 3. Evidently, Ruby is much more than just a calculator, but try outputting the result of 45628 multiplied by 7839 in a sentence by using string interpolation.
puts "The result of 45628 multiplied by 7839 is #{45628 * 7839}."
# 4. (true && false) || (false && true) || !(false && false)
# FALSE || FALSE || TRUE
# TRUE
# if true
# puts "I get printed!"
# end
x = 3
y = -4
def check_sign(number)
if number > 0
"#{number} is positive"
else
"#{number} is negative"
end
end
check_sign(x) # 3 is positive
check_sign(y) # -4 is negative
x = 3
y = 4
if x > y
puts "x is greater than y!"
elsif x < y
puts "x is less than y!"
else
puts "x equals y!"
end
if x != 10
puts "I get printed!"
end
# You can instead use unless, which is equivalent to "if not":
unless x == 10
puts "I get printed!"
end
counter = 1
while counter < 4
puts "Counter currently at #{counter}."
counter += 1 # Increment the counter
end
counter = 3
until counter == 0
puts "Counter currently at #{counter}."
counter -= 1
end
# Counter currently at 3.
# Counter currently at 2.
# Counter currently at 1.
3.times { puts "Chunky bacon!" }
# Chunky bacon!
# Chunky bacon!
# Chunky bacon!
one_to_ten = (1..10)
one_to_ten.each do |num|
print (num**2).to_s + " "
end
# 1 4 9 16 25 36 49 64 81 100