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 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 28,720 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