I am trying to write code that takes in an array of strings and returns the strings that are either less than 6 or end in "y", but not both.
The problem does not want a new array returned. I wrote code that works if I were to return a new array of strings that fit the conditions, but if I try to return just the strings and not an array, it doesn't work.
# frozen_string_literal: true
def select_long_words(words)
str = []
i = 0
while i < words.length
word = words[i]
if (word.length < 6 || word[-1] == 'y') && !(word.length < 6 && word[-1] == 'y')
str << word
end
i += 1
end
str
end
print select_long_words(%w[whatever are butterfly wit foreward funny])
puts
print select_long_words(%w[keepers cody])
This is the code that returns the new array of strings that fit the conditions.
zombiereturns..."). To highlight and indent a block of code, either indent it four spaces or select it and click on the icon{}above the edit box. Try it by editing your question now. To add to Jörg's comment, if an exception is raised tell us the line in which the error occurred, which is part of the error message.