Created
February 20, 2018 17:55
-
-
Save kat-perreira/dafde8a9237d2a807557fb7c94c4a7f1 to your computer and use it in GitHub Desktop.
Create a poll using Ruby : Favorite Fruit created by kaimana_Cat - https://repl.it/@kaimana_Cat/Create-a-poll-using-Ruby-Favorite-Fruit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#####Poll ##### | |
puts "What is your favorite type of fruit? I will collect ten votes" | |
#Get user input, store the entered values into a hash. | |
#Build hashmap for TWO VALUES, FOR NAME AND FOR COUNT | |
#Try to implement DRY | |
#Use each.with_object | |
#sample: (1..10).each_with_object([]) do |item, array| | |
#Set the hash to default zero | |
results = (1..10).each.with_object(Hash.new(0)) do |i, hash| | |
#Collect user input to variable "Candidate", assign it to the hash, use 1 += to count | |
candidate = gets.chomp | |
hash[candidate] += 1 | |
#puts to output name of vote | |
puts "Vote##{i}: <#{candidate}>" | |
end | |
#Process the results | |
puts "RESULTS...." | |
puts "Votes Summary:" | |
#each.do , votes being the hash candidate results | |
results.each do |candidate, votes| | |
puts "#{candidate} - #{votes} vote(s)" | |
end | |
#survey the map survey.map { |s| s.questions }.flatten.compact | |
#find short cut for Enumberable mix-in: | |
#winner = results.to_a.max { |a, b| a[1] <=> b[1] } | |
winner = results.max_by(&:last) | |
#output the winner | |
puts "WINNER: #{winner}!" | |
#Turn off computer and finally get some sleep zzzzzzzzz | |
#Take at least 24 hours off from code homework | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment