Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active November 21, 2018 10:49
Show Gist options
  • Select an option

  • Save harrisonmalone/dc59f0779079fdb4b7bf4287399deef7 to your computer and use it in GitHub Desktop.

Select an option

Save harrisonmalone/dc59f0779079fdb4b7bf4287399deef7 to your computer and use it in GitHub Desktop.
items = [{ customer: "John", item: "Soup", cost: 8.50}, {customer: "Sarah", item: "Pasta", cost: 12}, {customer: "John", item: "Coke", cost: 2.50}]
puts "whats your name?"
customer = gets.chomp
filter = items.select do |item|
item[:customer] == customer
end
cost = filter.map do |item|
item[:cost]
end
result = cost.sum
p "#{customer} owes $#{result}"
# this is a really nice challenge for working with select and map
# need to select only the items that contain the name and then just take the cost items out of that array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment