ruby - Can you append to specific elements in an array based on if statement conditions? -
i developer bootcamp student , having issue 1 of projects. using ruby code pig latin page. got passing tests until point needs accept multiple words:
def pig_latina(word) # univeral variables vowels = ['a','e','i','o','u'] user_output = "" # adds 'way' if word starts vowel if vowels.include?(word[0]) user_output = word + 'way' # moves first consonants @ beginning of word before vowel end else word.split("").each_with_index |letter, index| if vowels.include?(letter) user_output = word[index..-1] + word[0..index-1] + 'ay' break end end end # takes words start 'qu' , moves of bus , adds 'ay' if word[0,2] == 'qu' user_output = word[2..-1] + 'quay' end # takes words contain 'qu' , moves of bus , adds 'ay' if word[1,2] == 'qu' user_output = word[3..-1] + word[0] + 'quay' end # prints result user_output end
i don't know how it. isn't homework or anything. tried
words = phrase.split(" ") words.each |word| if vowels.include?(word[0]) word + 'way'
but think else
statement messing up. insight appreciated! thanks!!
def pig_latina(word) prefix = word[0, %w(a e o u).map{|vowel| "#{word}aeiou".index(vowel)}.min] prefix = 'qu' if word[0, 2] == 'qu' prefix.length == 0 ? "#{word}way" : "#{word[prefix.length..-1]}#{prefix}ay" end phrase = "the dog jumped on quail" translated = phrase.scan(/\w+/).map{|word| pig_latina(word)}.join(" ").capitalize puts translated # => "ethay ogday umpedjay overway ethay ailquay"
Comments
Post a Comment