Created
February 22, 2016 03:22
-
-
Save cyberrob-zz/3f6ca898957afdaa1380 to your computer and use it in GitHub Desktop.
ruby_assignment_area_code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
city_codes = { | |
'taipei' => 100, | |
'new york'=> 200, | |
'new orleans' => 300, | |
'houston' => 400, | |
'los angles' => 500, | |
'chicago' => 600, | |
'washington' => 700, | |
'memphis' => 800, | |
'london' => 900, | |
'dallas' => 1000 | |
} | |
# method to display city name | |
def displayAvailableCity(cities) | |
availableCities = '' | |
cities.each { | |
|key, value| availableCities += key + ', ' | |
} | |
puts availableCities | |
end | |
# method to get area code | |
def getCityCode(city, cities) | |
code = 0 | |
cities.each { |key, value| | |
# puts key == city | |
if key == city | |
code = value | |
break | |
else | |
code = -1 | |
end | |
} | |
if code == -1 | |
puts "Your input is INVALID!" | |
else | |
puts "Your input \"#{city}\" whose code is #{code}" | |
end | |
end | |
def firstQuestion() | |
puts "Do you want to check up city code?(Y/N)" | |
answer = gets.chomp | |
return answer | |
end | |
loop do | |
answer = firstQuestion() | |
if not answer.upcase.include? 'Y' | |
puts "Bye!" | |
break | |
end | |
puts "Type in one of these cities above: #{displayAvailableCity(city_codes)}" | |
city = gets.chomp | |
puts "#{getCityCode(city, city_codes)}" | |
#break | |
#answer = firstQuestion() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment