Ruby Ruby Loops Ruby Loops The Ruby Loop

Challenge 1 of 1 -Ruby Loops Need help

Im having trouble passing this Loops challenge. Any help would be much appreciated!

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

    if counter ==times
      break

  end
end

2 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! I feel like you're doing fairly well here, so I'm going to give some hints:

  • You're supposed to print the value of string that was passed into the function, but you're trying to print a fail message
  • You add 1 to counter but never actually assign it back into counter. That calculation is sort of floating out in limbo.
  • There are two ways to add 1 to counter and reassign it back into counter
  • Your if statement also needs an end statement

I think maybe you can get it with these hints, but let me know if you're still stuck! :sparkles:

Ha! Totally just got it. I guess for some reason I thought that fail message was the text I was supposed to print? I reassigned the 1 back into the counter and then once I added "end" to my if statement that made it work. Thanks alot Jennifer! :)