-
[Ruby] exercise _ Reverse Wordscoding for food 2022. 8. 2. 07:36
exercise
def reverse_words(sent) end puts reverse_words('keep coding') # => 'peek gnidoc' puts reverse_words('simplicity is prerequisite for reliability') # => 'yticilpmis si etisiuqererp rof ytilibailer'
my code
def reverse_words(sent) newsent = [] word = sent.split(" ") word.each do |char| newword = char.reverse newsent << newword end return newsent.join(" ") end puts reverse_words('keep coding') # => 'peek gnidoc' puts reverse_words('simplicity is prerequisite for reliability') # => 'yticilpmis si etisiuqererp rof ytilibailer'
solution
def reverse_words(sent) words = sent.split(" ") new_words = [] words.each { |word| new_words << word.reverse } new_sent = new_words.join(" ") return new_sent end puts reverse_words('keep coding') # => 'peek gnidoc' puts reverse_words('simplicity is prerequisite for reliability') # => 'yticilpmis si etisiuqererp rof ytilibailer'