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

Again this baffles me. What is wrong with my code?

Where do i go wrong here in this look is it the if statement, the counter being added or the loop itself?

loop.rb
def repeat(string, times)
  fail "times must be 1 or more" if times < 1
  counter = 0
  loop do
    # YOUR CODE HERE
    repeat("hi", 5)
    counter = counter + 1
    if counter == times
      break
    end
  end
end

2 Answers

You're over complicating it, the way to do it is replace repeat("hi",5) with print(string), you made it call the method inside the method you're trying to call which is why its failing.

Also instead of counter = counter + 1 put counter += 1 which increments counter with the value after += which must be an int not a string so don't do "1" just do 1

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 27,586 Points

You are so close! Nice job. Let's build on that.

The ONLY thing I needed to change to make it work was the repeat and substitute it with puts which prints the string (without a newline) each time it goes through the loop.

def repeat(string, times)
  fail "times must be 1 or more" if times < 1
  counter = 0
  loop do
    # YOUR CODE HERE
    # this line prints the string (once each loop interation)
    # this was the ONLY change you needed.
    puts(string)
    counter = counter + 1
    if counter == times
      break
    end   
  end
end

Thanks but this one is already solved