What's Wrong with My Code?


#1

https://www.codecademy.com/courses/ruby-beginner-en-MxXx5/0/6?curriculum_id=5059f8619189a5000201fbcb

This is the instruction given to me:
After each variable assignment: first_name, last_name, and city add the .capitalize! method
For state use the .upcase! method

print "What's your first name?"
first_name = gets.chomp
first_name = first_name.capitalize
first_name.capitalize!
print "What's your last name?"
last_name = gets.chomp
last_name = last_name.capitalize
last_name.capitalize!
print "Which city are you from?"
city = gets.chomp
city = city.capitalize
city.capitalize
print "What is your state/province's name? (eg. New York = NY)"
state = gets.chomp
state = state.upcase
state.upcase!

What's wrong? Please do tell me ASAP.
Thanks,
Urvashi.


#2

Since the capitalize! method with a ! at the end permanently modifies the string it's called on, there's no need for first_name = first_name.capitalize followed by first_name.capitalize!, etc.

You can just do:

first_name.capitalize!
...
last_name.capitalize!
...
city.capitalize!

Hopefully that helps!


#3

Thanks so much! :slight_smile: :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.