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!

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 The Ruby Loop

How do we equate counter to "times", break out of the loop and complete the method.

Is there some typo in the question. How can counter ever be equal to "times". I am not able to complete this challenge.

def repeat(string)

    loop do
    print string
    counter += 1

    if counter == 4



1 Answer

Jay McGavren
Jay McGavren
Treehouse Teacher

Part of the problem is that you seem to have deleted the times parameter, as well as some other bits of the default code. You should leave all that as it was when you started the challenge. If you ever find yourself in that situation in the future, click the Restart button to reset the challenge code to its starting state.

You printed string and incremented counter correctly, so you're almost there. Below, I've combined your code with the starting code, and added some comments. Hope this helps!

# Don't remove the "times" parameter. It needs
# to be here so someone calling "repeat" can
# specify how many times it should repeat.
def repeat(string, times)
  # Don't remove this line; it needs to be here.
  # Notice the "if"; it will only fail if someone
  # sets the "times" argument to less than 1.
  fail "times must be 1 or more" if times < 1
  # Don't remove this line either; "counter"
  # needs to start with the value zero.
  counter = 0
  loop do
    print string # This is correct.
    counter += 1 # This is correct.
    # COMPARE counter TO times HERE: you're almost
    # done! If "counter" is equal to "times", then
    # call "break".