Ruby Ruby Loops Ruby Loops Ruby Loops

Don-Alex Antoine
Don-Alex Antoine
3,052 Points

I am getting a void value expression when I run this

def repeat(string, times)

  counter = 0

  loop do
    print string
    counter++

    if (counter == times)
      break
      end

    end
end

puts repeat(Alex, 3)

2 Answers

Jovanny Elias
Jovanny Elias
14,386 Points

I have not worked with ruby in a while, But all languages pretty much work the same except for the syntax. Anyways from what I can see is that in your if statement you are not returning anything you simply stop the the function from continuing.

If you wanted to print out part of the string or do something with it you need to add that prior to ending your function inside your if statement.

Can you let us know what you want your end result to be? This would help us give you more detailed feedback.

Jovanny Elias
Jovanny Elias
14,386 Points

if you just want to print the first 3 letters of Alex then you need to add an else if statement to specify what needs to be done because when your function starts the value of times and counter are both 0 so the function just ends. this needs to be added prior to your if statement you have now.

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Your repeat function doesn't return anything (well, not anything useful). So you shouldn't print its return value. Just write repeat(Alex, 3) instead of puts repeat(Alex, 3).