Skip to content

Instantly share code, notes, and snippets.

@sheland
Last active August 1, 2018 18:45
Show Gist options
  • Save sheland/7a18a13b6d156e8efba03db5e4699e8c to your computer and use it in GitHub Desktop.
Save sheland/7a18a13b6d156e8efba03db5e4699e8c to your computer and use it in GitHub Desktop.
#Day 6 Exercises
#Updated 7/30/18
#1
#Ask the user to enter the names, ages, and favorite colors of their closest friends
#(you may not assume that the user's close friends all have unique names).
#Output the total number of close friends under 18, followed by their names.
#Output the number of unique favorite colors, and then list them
friends = []
under_age = []
colors = []
puts "Welcome to Hash Fun!"
puts "You'll be entering your friend's name, age and favorite color"
puts "how many friends do you have?"
friend = gets.chomp.to_i
friend.times do #x(based on user input) runs a times loop
friends_hash = {} #every time the loop run a friends_hash is created
#inputs friend's names
puts "What's your friend's name?"
friend_name = gets.chomp
friends_hash["name"] = friend_name
#inputs friend's ages
puts "What's your friend's age?"
age = gets.chomp.to_i
friends_hash["age"] = age
#calulates underaged friends
if age < 18
under_age << friend_name
end
#inputs friend's color
puts "What's your friend's favorite color?"
color = gets.chomp
friends_hash["color"]
colors << color
#displays name, age, color
print "You have #{under_age.count} friends that's younger than 18, #{under_age} years old"
puts " Your friend's favorite colors are #{colors.uniq}"
end
@shrutivanw
Copy link

Nice work! Just a few comments for further improvement:

  • You are currently printing the summary information on number of friends that are younger than 18 and unique colors, everytime a new friend's information is entered. This should be outside the loop.
  • Line 40 just says friends_hash["color"] Nothing gets assigned to friends_hash["color"].
  • As we saw on day 6, Rubyist prefer the symbol notation, i.e. using symbols as keys for hashes instead of using strings as keys for hashes since it uses less memory and is often faster look up. So, use friends_hash[:color] rather than friends_hash["color"]
  • You don't need to save user input into an intermittent variable if you're immediately going to put it into a hash next.
  • Adhere to Ruby style guidelines for comments and indentation.

Applying the above comments, here's what your code will look like:

# Day 6 Exercises 
# Updated 7/30/18

# 1
# Ask the user to enter the names, ages, and favorite colors of their closest friends 
# (you may not assume that the user's close friends all have unique names).
# Output the total number of close friends under 18, followed by their names. 
# Output the number of unique favorite colors, and then list them 

friends = []
under_age = []
colors = []

puts "Welcome to Hash Fun!"
puts "You'll be entering your friend's name, age and favorite color"
puts "how many friends do you have?"
friend = gets.chomp.to_i

friend.times do  # x(based on user input) runs a times loop
  friends_hash = {} # every time the loop run a friends_hash is created
  # inputs friend's names
  puts "What's your friend's name?"
  friends_hash[:name] = gets.chomp
  
  # inputs friend's ages
  puts "What's your friend's age?"
  friends_hash[:age] = gets.chomp.to_i
  
  # calulates underaged friends 
  if friends_hash[:age] < 18
    under_age << friends_hash[:name]
  end
  
  # inputs friend's color
  puts "What's your friend's favorite color?"
  friends_hash[:color] = gets.chomp
  colors << friends_hash[:color]
end

# displays name, age, color 
print "You have #{under_age.count} friends that's younger than 18, #{under_age} years old"
puts " Your friend's favorite colors are #{colors.uniq}"

Can you see how fixing the indentation makes the code easier to read and hence makes i easier to spot error like the summary display should be outside the loop?

Finally, for additional practice, try having two more separate loops. The first loop just takes in user input and saves it in the array of hashes. The second loop saves all the colors from the hash into a separate array of colors and the third loop examines each of the ages in the array of hashes and creates the under_age array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment