ruby - CodeCademy 4.6 bonus exercise -
hello guys tryng bonus exercise 4.6
"what make sure redactor redacts word regardless of whether it's upper case or lower case?"
(link = https://www.codecademy.com/courses/ruby-beginner-en-mzrz6/0/6?curriculum_id=5059f8619189a5000201fbcb)
this code
puts "tell me text for" text = gets.chomp puts "tell me word censor" word = gets.chomp text_array = text.split(" ") text_array.each |parole| if parole == ( word.downcase || word.capitalize ) puts "censure".upcase else puts "#{parole}" end end
why wrong? plz not give me total different code understand why mine uncorrect, , how correct, thanks
i think main thing here you're misunderstanding how ||
or operator used.
if parole == ( word.downcase || word.capitalize )
isn't way check if parole
either equal word.downcase
or equal word.capitalize
. ||
operator says overall expression considered true if either side of ||
true. need write out both conditions in full e.g.
if parole == word.downcase || parole == word.capitalize
also, looks you're misunderstanding capitalize
vs. upcase
. capitalize
returns string ensuring first letter capital , rest lowercase, whereas upcase
returns string in capitals.
so use:
if parole == word.downcase || parole == word.upcase
this work long parole
entered in caps or lowercase.
alternatively, ensure parole
in lower case:
parole = parole.downcase
and 1 condition needed:
if parole == word.downcase
as sagarpandya82 mentioned there other approaches using gsub
etc. suggestions above closest in terms of fixing original code.
Comments
Post a Comment