Skip to content

Instantly share code, notes, and snippets.

@richcsmith
Last active August 29, 2015 13:57
Show Gist options
  • Save richcsmith/9716356 to your computer and use it in GitHub Desktop.
Save richcsmith/9716356 to your computer and use it in GitHub Desktop.
# Step 1: Working with Arrays and Hashes
# Copy your solution from ex_teddit_conditional.rb in 02_Variables_Conditionals
# Create an empty stories array. This will be used to hold all stories we capture.
# Users enter the story as they do now, but it gets put in a hash instead.
# Update any reference to the story (upvotes, category and title)
# Your story hash should look like the one below:
# { title: "Monkeys thank mayor for flounder tooth necklace", category: "Teeth", upvotes: 1 }
# Add the hash to an array called stories and print "Story: Monkeys thank mayor for flounder tooth necklace, Category: (Teeth), Current Upvotes: 1"
# Using the stories array
# Test your cat, bacon, and food upvote conditional logic.
#
# Step 2: Adding Loops
# Use a loop, so that your program continues to ask a user for stories until they chose to quit. ("Would you like to add another story? Enter 'y' or 'n'")
# Once the user is finished with entering their stories, use .each to print each story in the stories array.
#
#
def get_input
puts "Please enter a News Story:"
story = gets.chomp
puts "What is the category?"
category = gets.chomp
puts "How many upvotes would you like to give?"
upvote = gets.chomp.to_i
story_hash = {
:story => story,
:category => category,
:upvotes => upvote
}
end
def create_array(stories, story)
stories.push(story)
puts stories
end
def calculate_upvotes(story, category, upvote)
if category.downcase.include? 'cats'
upvote *=3
elsif category.downcase.include? 'bacon'
upvote *= 8
elsif category.downcase.include? 'food'
upvote *= 3
end
end
puts "Welcome to Teddit! A text-based news aggregator. Get today's news tomorrow!"
stories = []
user_input = "yes"
begin
puts "Would you like to create a news story?"
user_input = gets.chomp
if user_input != "no"
story = get_input
create_array(stories, story)
else
end
end
# upvotes = calculate_upvotes(story, category)
# puts "New story added! #{story}, Category: #{category.capitalize}, Current upvotes: #{upvotes}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment