gurwinder singh gurwinder singh - 2 months ago 6
Ruby Question

Why am I getting an infinite loop? should the "i += 1" be at a different place?

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] = ('')

i += 1

end

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.