Ruby Ruby Loops Ruby Loops The Ruby Loop

Shreemangal Sethi
Shreemangal Sethi
Full Stack JavaScript Techdegree Student 8,385 Points

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