I'm trying to make a code that returns all the even values in a string. The code I created seems to do that, but it won't return the white spaces and so the final test fails. Can someone help me to understand why it returns all the letters, but none of the whitespaces?
# Every Other Letter Define a method, #every_other_letter(string),
# that accepts a string as an argument. This method should return a
# new string that contains every other letter of the original string,
# starting with the first character. Treat white-space and punctuation
# the same as letters.
def every_other_letter(string)
idx = 0
final = []
while idx < string.length
letters = string[idx].split
final = final + letters
idx = idx + 2
end
p final = final.join
end
puts "------Every Other Letter------"
puts every_other_letter("abcde") == "ace"
puts every_other_letter("i heart ruby") == "ihatrb"
puts every_other_letter("an apple a day...") == "a pl a.."
and this returns:
------Every Other Letter------
"ace"
true
"ihatrb"
true
"apla.."
false
=> nil