diff --git a/enumerables.rb b/enumerables.rb index b431b85dc..c5e01be96 100644 --- a/enumerables.rb +++ b/enumerables.rb @@ -9,49 +9,46 @@ def spicy_foods ] end -# 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 do |food| + food[:name] + end 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 do |food| + food[:heat_level] > 5 + end end -# given an array of spicy foods, **output to the terminal** -# each spicy food in the following format: -# Buffalo Wings (American) | Heat Level: 🌶🌶🌶 -# 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| + 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 do |food| + cuisine == (food[:cuisine]) + end 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 do |lowest, highest| + lowest[:heat_level] - highest[:heat_level] + end 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.each do |display| + if display[:heat_level] > 5 + puts "#{display[:name]} (#{display[:cuisine]}) | Heat Level: " + "🌶" * display[: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 + average_heat = spicy_foods.sum do |food| food[:heat_level] + end + average_heat / spicy_foods.length end