-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalkathon.rb
47 lines (39 loc) · 1.36 KB
/
walkathon.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
puts "Welcome to Kristy's walkathon program*! *not valid for marathons"
walkathon = {}
laps = []
money = []
print "What is your earning goal for this event? :"
walkathon[:goal] = gets.chomp.to_f
puts "Awesome! Good luck!\n"
print "How much will each person earn per lap? :"
walkathon[:per_lap] = gets.chomp.to_f
puts "Hurray you finished! How many laps did each of the five team members complete?: "
5.times do
laps.push(gets.chomp.to_i)
end
walkathon[:laps] = laps
walkathon[:laps].each_with_index do |laps, i|
earned = laps * walkathon[:per_lap]
puts "Walker number #{i + 1} did #{laps} laps:\n earning a total of $" + sprintf('%.2f', earned) + "!"
money.push(earned)
end
if money.count(money.max) > 1
winners = ""
money.each_with_index do |earned, i|
if earned == money.max
winners += "\nWalker Number #{i + 1}"
end
end
puts "We have a tie! Your winners are:" + winners
else
puts "Walker number #{money.index(money.max) + 1} earned the most money!"
end
total = money.inject(0) { |sum, x| sum + x }
puts "The total amount earned is $" + sprintf('%.2f', total) + "."
if total > walkathon[:goal]
puts "You beat your goal by $" + sprintf('%.2f', total - walkathon[:goal]) + "!"
elsif total == walkathon[:goal]
puts "You met the goal!"
else
puts "You missed your goal by $" + sprintf('%.2f', walkathon[:goal] - total) + ". :-("
end