Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save solomoncarnes/08ce364669b763e01e601f4707afeb1f to your computer and use it in GitHub Desktop.
Save solomoncarnes/08ce364669b763e01e601f4707afeb1f to your computer and use it in GitHub Desktop.
Trip Calculator (No defined number of trips)
# asks the user how many trips they've taken and prompts them that number of times
puts "How many trips did you take?"
trip_count = gets.chomp.to_i
# prompts the user the trip locations and stores them in a hash
trip_data = trip_count.times.map do |x|
puts "Where did you go for your trip ##{x+1}?"
trip_location = gets.chomp
puts "How much did you spend on lodging?"
lodging_cost = gets.chomp.to_i
puts "How much did you spend on round-trip transportation?"
transit_cost = gets.chomp.to_i
puts "How many bags did you check?"
bags_checked = gets.chomp.to_i
{
location: trip_location,
lodging: lodging_cost,
transit: transit_cost,
bags: bags_checked *50
}
end
#total spent on all trips
total_spent = trip_data.reduce(0) do |sum, trip| sum + trip[:lodging] + trip[:transit] + trip[:bags]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment