Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Loops Ruby Loops Ruby Loops

I am getting a void value expression when I run this

def repeat(string, times)

  counter = 0

  loop do
    print string

    if (counter == times)


puts repeat(Alex, 3)

2 Answers

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.

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
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).