0

I'm new to Ruby programming language and i am asked to make a small program that does the following: Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.

Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.

but in my if else statement it doesn't go into the if even if its true it stays at the else statement

i have tried taking the string and converting it into an array and work on the array and tried working on the string as is

def translate (str)
    i = 0
    while i < str.length
        if (str[i] == "a" or str[i] == "e" or str[i] == "o" or str[i] == "u" or str[i] == "i")
            str = str + "ay"
            return str
        else
            temp = str[0...1]
            str = str[1...str.length]
            str = str + temp
        end
        i = i + 1
    end

end

s = translate("banana")
puts s

the program doesn't enter the if statement at all and keeps getting into the else statement until the word returns the same with out any changes

steenslag
  • 79,051
  • 16
  • 138
  • 171
  • You'll want to use `||` rather than `or` for conditioning – Viktor Sep 08 '19 at 12:29
  • @Виктор What is the reason for that? – arcadeblast77 Sep 08 '19 at 12:31
  • 2
    @arcadeblast77 `or` has lower precedence than the `||` operator or even the `=` operator, so you sometimes get results you probably didn't intend to get like `bool = false or true` what do you think the value of `bool` is here ? – Viktor Sep 08 '19 at 12:36

3 Answers3

0

If I understand the problem correctly, you do not need to loop at all here. You just need to check the first letter, and not all of the letters.

def translate (str)
  if str[0] == 'a' or str[0] == 'e' or str[0] == 'o' or str[0] == 'u' or str[0] == 'i'
    str + 'ay'
  else
    temp = str[0...1]
    str = str[1...str.length]
    str = str + temp
    str + 'ay'
  end
end

By the way, I was able to figure this out with the debugger. Did you try that at all? Also, with your original code it turns out that for some inputs (like 'baaan'), your else statement does execute.

arcadeblast77
  • 560
  • 2
  • 12
0

Aside from my suggestion to use || instead of or, your method doesn't need a #while iterator since you're checking only for the first letter. The if/else statement should be executed only once.

You can also replace all the checks with a single #include? method like this:

def translate (str)
   if %w[a e i o u].include?(str[0])
       str + "ay"
   else
       str[1..-1] + str[0] + "ay"
   end
end

Notice that I've also removed the return statement since the last executed line will be returned, so either line 3 or line 5 in the method above. You can also add a ternary operator to make it in one line:

 %w(a e i o u).include?(str[0]) ? str + "ay" : str[1..-1] + str[0] + "ay"

Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.

translate("ana")
# ~> "anaay"

Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.

translate("banana")
# ~> "ananabay"
Viktor
  • 2,623
  • 3
  • 19
  • 28
  • my while loop was for when the constant sound is more than one letter, but when i put it in my code works, i think i should've stated that in more detail in Rule 2 : to move the whole constant sound – Hussien Idris Sep 09 '19 at 08:08
  • Even if you wanted to move 2 letters at the end of the word, you still wouldn't use a while loop, you'd just make a condition to check if you're moving 1 or 2 letters :) Something like `new_string = old_string[2..-1] + old_string[0..1]` – Viktor Sep 09 '19 at 08:23
0

I don't see a problem with or or || in this case. The problem I see is that if the start letter is a consonant, you changing str rotating it's letters at each iteration (see the commented part of the code), so the starting letter is never a vowel.

Then you are missing a returning value at the end so it returns nil and puts nothing.

def translate (str)
    i = 0
    while i < str.length
        p str[i] # it's never a vowel
        if (str[i] == "a" or str[i] == "e" or str[i] == "o" or str[i] == "u" or str[i] == "i")
            str = str + "ay"
            return str
        else # here you are rotating the word
            temp = str[0...1]
            str = str[1...str.length]
            str = str + temp
            p str
        end
        i = i + 1
    end
    # missing a return value
end

s = translate("banana")
p s

So it prints out:

# "b"
# "ananab"
# "n"
# "nanaba"
# "n"
# "anaban"
# "b"
# "nabana"
# "n"
# "abanan"
# "n"
# "banana"
# nil

The code works correctly in case the first letter is a vowel, so it enters the if true:

s = translate("ananas")
p s
#=> "ananasay"

By the way, as already posted by others, you don't need any while loop. Just checking the first letter with an if statement is enough.

iGian
  • 11,023
  • 3
  • 21
  • 36
  • While there might not be a problem with using `or` and `and` in this example, they're [banned by the official ruby style guide](https://github.com/rubocop-hq/ruby-style-guide/commit/5920497452c1f6f604742a735f5684e86d4c0003). I don't think OP is aware of both `or` and `||` and chose `or` for some reason, I think it's quite the opposite, he's probably learning Ruby as his first language or comes from a language where `or` has the same effect like `||` in Ruby. While your answer works and is correct I still don't see any reason to use `or` over `||`, just my opinion :) – Viktor Sep 08 '19 at 19:18
  • 1
    @Виктор I agree with you, just in this case it doesn't prevent the code from working. – iGian Sep 09 '19 at 07:02