Victor Mora Victor Mora - 5 months ago 70
Ruby Question

Method to reverse string only if it has less than four letters

I need to write a ruby method that reverses a string only if it has less than four characters.

# Write a method that reverses
# a string ONLY if it's shorter than
# 4 letters.
# Otherwise, the string is
# returned as-is.
# (Hint: strings have
# a built-in .length method!)
# conditional_reverse("yo")
# => "oy"
# conditional_reverse("hello")
# => "hello"


Here is the code I came up with.

def conditional_reverse(string)
good = string.length
if good < 4
puts string.reverse
else
puts string
end
puts conditional_reverse("cat")
end


When I run it in repl I get the following response

:conditional_reverse


I have no idea what i'm doing wrong.

Answer

just put puts conditional_reverse("cat") out side our def

def conditional_reverse(string)
  good = string.length 
  if good < 4 
    puts string.reverse
  else 
    puts string
  end 
end

conditional_reverse("cat")
Comments