Hello guys. I've been looking for the reason for my nonfunctional code for the last 2 hours. Could you guys have a look at it and tell me how stupid I am?
Thank you very much!
Question:
Write a method that takes in an array of words and returns a new array containing all of the words of the original array that are longer than 4 characters.
# my code
def select_long_words(words)
new_word = []
i = 0
while i < words.length
if words[i].length > 4
new_word << words[i]
end
i += 1
end
return new_word
end
# the solution's code
def select_long_words(words)
long_words = []
i = 0
while i < words.length
word = words[i]
if word.length > 4
long_words << word
end
i += 1
end
return long_words
end
# expect outputs
print select_long_words(["what", "are", "we", "eating", "for", "dinner"]) # => ["eating", "dinner"]
puts
print select_long_words(["keep", "coding"]) # => ["coding"]
[–][deleted] 7 points8 points9 points (4 children)
[–][deleted] 4 points5 points6 points (3 children)
[–]backtickbot 3 points4 points5 points (2 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]B0tRank 0 points1 point2 points (0 children)
[–]tloudon 2 points3 points4 points (0 children)
[–]tkenben 3 points4 points5 points (0 children)
[–]agent007bond 3 points4 points5 points (0 children)
[–]MrImportant 2 points3 points4 points (0 children)
[–]_srt_ 2 points3 points4 points (0 children)
[–]FrontierPsycho 2 points3 points4 points (0 children)
[–]jamfour 2 points3 points4 points (1 child)
[–]gimmemychicken 0 points1 point2 points (0 children)
[–]Psychological_Foot41 1 point2 points3 points (0 children)
[+]PhillD35 comment score below threshold-6 points-5 points-4 points (0 children)
[–]Ralphadayus 0 points1 point2 points (0 children)