/
ex21.rb
42 lines (32 loc) · 953 Bytes
/
ex21.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
def add(a, b)
puts "ADDING #{a} + #{b}"
a + b
end
def subtract(a, b)
puts "SUBTRACTING #{a} - #{b}"
a - b
end
def multiply(a, b)
puts "MULTIPLYING #{a} * #{b}"
a * b
end
def divide(a, b)
puts "DIVIDING #{a} / #{b}"
a / b
end
puts "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78,4)
weight = multiply(90, 2)
iq = divide(100, 2)
puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}"
# A puzzle for the extra credit, type it in anyway.
puts "Here is a puzzle."
# This is equal to 35 + (74 - (180 * (50 / 2)) )
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
puts "That becomes: #{what} Can you do it by hand?"
# I can, in fact.
# EC 4
puts "Now I'll do the opposite. 50 + (180 - (35 / 74)), which should be 229 or around there with integer division."
solution = add(iq, subtract(weight, divide(age, height)))
puts "And using the methods we've created, we get #{solution}."