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

Lee Gentry
Lee Gentry
6,364 Points

Bummer! We expected repeat("hi", 5) to print "hi" 5 times, but instead we got "hihihihihi"!

I've slammed my head against this challenge for the past two hours and can't figure out what is being asked of me.

I tried all different types of formatting... puts over print... I tried printing out from different parts of the method...

There wasn't even any reference to the string "hi" or the times integer needing to be at 5 in the challenge to begin with. What's that about?

I'm guessing it's something syntactically minor, but I couldn't even figure out how to google my way out of this one.

What am I doing wrong?

  • Lee
loop.rb
def repeat(string, times)
  string = "hi"
  times = 5
  counter = 0

  loop do
    print(string)
    counter += 1
    if counter == times
      break
    end
  end
end

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Would it be comforting to know that you're actually doing splendidly? Your syntax and logic are spot on! Seriously, they are. However, you are resetting the values that Treehouse is sending in. They're going to determine the value of string and the value of times.

So if I simply remove:

  string = "hi"
  times = 5

... your code passes with flying colors! You can think of parameters in the definition of a function or method as local variable declarations. They are going to accept some piece of information from another piece of code. Something is going to send in the string they want to be printed and the number of times to be printed. By hard coding those values your code will always print out "hi" exactly 5 times. But if we keep it more generic then another piece of code can send in "Lee" and 10 and it will print out "Lee" ten times.

Hope this helps clarify things! :sparkles:

Lee Gentry
Lee Gentry
6,364 Points

Thank you, Jennifer! Comforting and informative! ?????