Skip to content
26 changes: 19 additions & 7 deletions enumerables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ def spicy_foods
# given an array of spicy foods, **return an array of strings**
# with the names of each spicy food
def get_names(spicy_foods)
# your code here
spicy_foods.map { |food| food[:name]}
end

# given an array of spicy foods, **return an array of hashes**
# where the heat level of the food is greater than 5
def spiciest_foods(spicy_foods)
# your code here
spicy_foods.filter { |food| food[:heat_level] > 5}

end

# given an array of spicy foods, **output to the terminal**
Expand All @@ -27,31 +28,42 @@ def spiciest_foods(spicy_foods)
# HINT: you can use * with a string to produce the correct number of 🌶 emoji.
# "hello" * 3 == "hellohellohello"
def print_spicy_foods(spicy_foods)
# your code here
spicy_foods.each do |food|
food[:heat_level] = "#{"🌶" * food[:heat_level]}"
puts "#{food[:name]} (#{food[:cuisine]}) | Heat Level: #{food[:heat_level]}"
end
end

# given an array of spicy foods and a string representing a cuisine, **return a single hash**
# for the spicy food whose cuisine matches the cuisine being passed to the method
def get_spicy_food_by_cuisine(spicy_foods, cuisine)
# your code here
spicy_foods.find { |food| food[:cuisine] == cuisine}
end

# Given an array of spicy foods, **return an array of hashes**
# sorted by heat level from lowest to highest
def sort_by_heat(spicy_foods)
# your code here
spicy_foods.sort_by { |food| food[:heat_level] }

end

# given an array of spicy foods, output to the terminal ONLY
# the spicy foods that have a heat level greater than 5, in the following format:
# Buffalo Wings (American) | Heat Level: 🌶🌶🌶
# HINT: Try to use methods you've already written to solve this!
def print_spiciest_foods(spicy_foods)
# your code here
spicy_foods.filter do |food|
if food[:heat_level] > 5
puts "#{food[:name]} (#{food[:cuisine]}) | Heat Level: #{"🌶" * food[:heat_level]}"
end
end
end

# given an array of spicy foods, return an integer representing
# the average heat level of all the spicy foods in the array
def average_heat_level(spicy_foods)
# your code here
new_arr = spicy_foods.map do |food|
food[:heat_level]
end
new_arr.reduce(0, :+) / new_arr.length
Comment on lines +65 to +68

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good candidate for #sum

end