Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

loop.rb
def repeat(string)
  counter

    loop do
    print string
    counter += 1

    if counter == 4
      break
    end

  end

end

1 Answer

Jay McGavren
STAFF
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".
  end
end