Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 13 additions & 28 deletions enumerables.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,42 @@
require 'pry'

# this method returns an array of hashes, which we'll use in the other methods
def spicy_foods
def spicy_foods
[
{ name: 'Green Curry', cuisine: 'Thai', heat_level: 9 },
{ name: 'Buffalo Wings', cuisine: 'American', heat_level: 3 },
{ name: 'Mapo Tofu', cuisine: 'Sichuan', heat_level: 6 }
]
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 { |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**
# 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 { |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
spiciest_foods = spiciest_foods(spicy_foods)
print_spicy_foods(spiciest_foods)
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
sum_of_heat_level = spicy_foods.sum { |food| food[:heat_level] }

sum_of_heat_level / spicy_foods.count
end
20 changes: 10 additions & 10 deletions spec/enumerables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
end

describe '#spiciest_foods' do

it 'return an array of hashes where the heat level of the food is greater than 5' do
expect(spiciest_foods(spicy_foods)).to eq([
{ name: 'Green Curry', cuisine: 'Thai', heat_level: 9 },
{ name: 'Mapo Tofu', cuisine: 'Sichuan', heat_level: 6 }
{ name: 'Mapo Tofu', cuisine: 'Sichuan', heat_level: 6 }
])
end

end

describe '#print_spicy_foods' do

it 'outputs the spicy foods to the terminal in the specified format' do
output_str = "Green Curry (Thai) | Heat Level: 🌶🌶🌶🌶🌶🌶🌶🌶🌶\nBuffalo Wings (American) | Heat Level: 🌶🌶🌶\nMapo Tofu (Sichuan) | Heat Level: 🌶🌶🌶🌶🌶🌶\n"
expect { print_spicy_foods(spicy_foods) }.to output(output_str).to_stdout
end

end

describe '#get_spicy_food_by_cuisine' do

it 'returns a single hash for the spicy food whose cuisine matches the cuisine being passed to the method' do
expect(get_spicy_food_by_cuisine(spicy_foods, "Sichuan")).to eq({ name: 'Mapo Tofu', cuisine: 'Sichuan', heat_level: 6 })
expect(get_spicy_food_by_cuisine(spicy_foods, "Thai")).to eq({ name: 'Green Curry', cuisine: 'Thai', heat_level: 9 })
Expand All @@ -39,7 +39,7 @@
end

describe '#sort_by_heat' do

it 'return an array of hashes sorted by heat level from lowest to highest' do
sorted_array = [
{ name: 'Buffalo Wings', cuisine: 'American', heat_level: 3 },
Expand All @@ -52,18 +52,18 @@
end

describe '#print_spiciest_foods' do

it 'outputs ONLY the spicy foods that have a heat level greater than 5 to the terminal in the specified format' do
output_str = "Green Curry (Thai) | Heat Level: 🌶🌶🌶🌶🌶🌶🌶🌶🌶\nMapo Tofu (Sichuan) | Heat Level: 🌶🌶🌶🌶🌶🌶\n"
expect { print_spiciest_foods(spicy_foods) }.to output(output_str).to_stdout
end

end

describe '#average_heat_level' do

it 'returns an integer representing the average heat level of all the spicy foods in the array' do
expect(average_heat_level(spicy_foods)).to eq(6)
end

end