Quotes help make search much faster. Example: "Practice Makes Perfect"

Monday, January 14, 2013

Codecademy Sample Solution: Delete



movies = {
Tron: 4,
Jaws: 3,
Inception: 5
}

puts "Enter a choice ('add', 'update', 'display', 'delete'):"
choice = gets.chomp.downcase
#puts choice
case choice
when 'add'
puts "Enter the movie title you'ld like to add:"
title = gets.chomp
if movies[title.to_sym] === nil
puts "Enter the movie's rating:"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
#test
puts movies
puts "Movie title and rating has been added!"
else
puts movies
puts "Movie already exists!"
end
when 'update'
puts "Enter the movie title you'ld like to update:"
title = gets.chomp
if movies[title.to_sym] === nil
puts movies
puts "Movie doesn't exist!"
else
puts movies
puts "Enter the movie's updated rating:"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
#test
puts movies
puts "Movie rating has been updated!"
end
when 'display'
movies.each_pair do |key, value|
puts "#{key}: #{value}"
end
when 'delete'
puts movies
puts "Enter the movie title you'ld like to delete:"
title = gets.chomp
if movies[title.to_sym] === nil
puts "That movie doesn't exist!"
else
puts movies
movies.delete(title.to_sym)
puts "Movie has been deleted!"
puts movies
end
else
puts "Error!"
end

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts