NicholasJay
NicholasJay

Reputation: 37

How do I join the final result so it is not an array, but a string?

I've written a method for adding ay to the ends of words if they begin with a vowel. If the words begin with a consonant it will move the consonants to the end of the word and then add ay.

My issue with this is that my result is returned in an array for example: translate("happy animals") Instead of getting "appyhay animalsay" I get ["appyhay", "animalsay"] I tried joining them at the end, but when I run the test it says that the join method could not be found? Is this just a mess or am I getting close? Many thanks for any insight :)

def translate(word)
  multiplewords = word.split(" ")
multiplewords.map! do |x|
    separated = x.split("")
        if !'aeiou'.include?(separated[0])
            while !'aeiou'.include?(separated[0])
                letter = separated.shift
                separated << letter
                separated
            end
            final = separated.join("") + "ay"
        else
            final = separated.join("") + "ay"
        end
    end
end

translate("happy animals") => ['appyhay', 'animlasay']

Answer needed: "appyhay animalsay"

Upvotes: 1

Views: 58

Answers (1)

konsolebox
konsolebox

Reputation: 75488

You should join it at the last part. I tried to simplify it a bit as well.

#!/usr/bin/env ruby

def translate(word)
  word.split(" ").map do |x|
    separated = x.split("")
    if !'aeiou'.include?(separated[0])
      while !'aeiou'.include?(separated[0])
        letter = separated.shift
        separated << letter
      end
    end
    separated.join("") + "ay"
  end.join(' ')
end

puts translate("happy animals") 

Output:

appyhay animalsay

Upvotes: 2

Related Questions