gurwinder singh gurwinder singh - 2 months ago 5
Ruby Question

My code returns with all vowels removed instead of just the last one removed

In this code I am trying to remove the last vowel in the word. Once I ran the code, I received an argument error stating "comparison of Fixnum with Array failed(ArgumentError). Please help!

VOWELS = %w( a e i o u)

def hipsterfy(string)

new_string = string.split('')

reversed_string = new_string.reverse

i = 0

while i <= reversed_string.length

if VOWELS.include?(reversed_string[i])

reversed_string[i] = ('')

end

i += 1
reversed_string

end

reversed_string.reverse

end

Answer

I am sure this is not the only glith with this code, but the error you got came from here:

while i <= reversed_string

it probably should be

while i < reversed_string.length

since reversed_array is an array, and you probably want to compare i against it’s length.

Comments