0

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

     if VOWELS.include?[i]

        reversed_string[i] = ('')

    i += 1
    end
    reversed_string
  end

  reversed_string.reverse 

end 
Matt
  • 74,352
  • 26
  • 153
  • 180
  • Hint: If you run out of vowels does `i` get incremented? You also need to dig into regular expressions or, at the very least, the [`tr`](https://ruby-doc.org/core-2.2.0/String.html#method-i-tr) method. – tadman Sep 22 '16 at 18:19
  • Correct indentation would almost certainly help you. – pjs Sep 22 '16 at 18:21
  • Though it does not answer your question, your problem is addressed in [this SO question](http://stackoverflow.com/questions/39628583/how-do-i-remove-the-last-vowel-in-a-string-in-ruby). – Cary Swoveland Sep 22 '16 at 18:29
  • Please do not edit your question such that it invalidates existing answers. Instead, create a new question. – Matt Oct 12 '16 at 19:02

3 Answers3

1

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.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
1

When you test

if VOWELS.include?[i]

you're checking if the VOWELS constant includes the array index. You want to look at the letter at that point in the string instead, right?

if VOWELS.include?(reversed_string[i])
pjmorse
  • 9,204
  • 9
  • 54
  • 124
0

There needed to be a "break" statement after the if statement to stop the loop as soon as it looped the first vowel. That would prevent it from going on to the next vowel. PROBLEM SOLVED.